Using UIView-Positioning for iOS development to simplify page layout and iosuiview Animation

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.