使用过代码布局的人可能会有这样的感觉,给控件设置frame的时候比较繁琐。最 近在Github上看到有一个UIView的一个分类UIView-Positioning,这个分类提供了一些属性,比如left、right、 top、bottom、centerX、centerY等,在布局 ...
使用过代码布局的人可能会有这样的感觉,给控件设置frame的时候比较繁琐。最 近在Github上看到有一个UIView的一个分类UIView-Positioning,这个分类提供了一些属性,比如left、right、 top、bottom、centerX、centerY等,在布局的时候使用这些属性,会更简单和方便,下面介绍下具体使用。
UIView-Positioning的Github的地 址:https://github.com/freak4pc/UIView-Positioning,将UIView+Positioning.h和 UIView+Positioning.m文件拷贝到工程里面。
在使用代码布局的时候,我一般习惯按照下面三个步骤去做。
1、声明控件变量。
@implementation LoginView{ UILabel *_userNameLabel; UITextField *_userNameField;}
- (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { _userNameLabel = [UILabel new]; _userNameLabel.font = [UIFont systemFontOfSize:14.0]; _userNameLabel.textColor = [UIColor blackColor]; _userNameLabel.backgroundColor = [UIColor clearColor]; _userNameLabel.text = @"用户名:"; [self addSubview:_userNameLabel]; _userNameField = [UITextField new]; _userNameField.font = [UIFont systemFontOfSize:14.0]; _userNameField.textColor = [UIColor blackColor]; _userNameField.borderStyle = UITextBorderStyleRoundedRect; [self addSubview:_userNameField]; } return self;}
3、在layoutSubViews方法里面对控件进行布局,下面使用 UIView-Positioning分类的size、left、top、bottom、centerY等属性,通过使用right属性,可以取到左边 Label控件的origin.x+size.width,然后加上一个padding值,就可以得到右边TextField控件的origin.x。平 时我们可能经常会碰到,要将两个不同高度的控件,设置为垂直方向对齐,我这里特意将这两个控件的高度设置得不一样,通过将它们的centerY属性设置为 相等,就可以保持这两个控件在垂直方向对齐了。
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:iOS开发之使用UIView
关键词:IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。