Using UIView-Positioning for iOS development to simplify page layout and iosuiview Animation
People who have used code layout may feel this way, and it is complicated to set frames for controls. As you can see on Github, there is a class of UIView-Positioning, which provides some attributes, such as left, right, top, bottom, centerX, and centerY, it is easier and convenient to use these attributes during layout. The following describes how to use these attributes.
Github address of UIView-Positioning: https://github.com/freak4pc/uiview-positioning. copy the uiview1_positioning.hand UIView + Positioning. m files to the project.
When using code layout, I usually use the following three steps.
1. Declare control variables.
@implementation LoginView{ UILabel *_userNameLabel; UITextField *_userNameField;}
2. In the initWithFrame method, create a control and set its basic attributes. Then, add the control to the View subview.
-(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 = @ "username:"; [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. layout the controls in the layoutSubViews method. The following uses the size, left, top, bottom, and centerY attributes of the UIView-Positioning classification and uses the right attribute, you can obtain the origin of the Label control on the left. x + size. width, and then add a padding value to obtain the origin of the TextField control on the right. x. We may often encounter two controls with different heights and set them to vertical alignment. Here I specially set these two controls to different heights, by setting their centerY attribute to equal, you can keep the two controls vertically aligned.
- (void)layoutSubviews{ [super layoutSubviews]; CGFloat margin = 50, padding = 5; _userNameLabel.size = CGSizeMake(60, 15); _userNameLabel.left = margin; _userNameLabel.top = margin; _userNameField.size = CGSizeMake(200, 30); _userNameField.left = _userNameLabel.right + padding; _userNameField.centerY = _userNameLabel.centerY;}
UIView-Positioning extends some attributes of UIView, which makes the code layout quite convenient. We recommend that you use it.