Using uiview-positioning for iOS development simplifies page layout

Source: Internet
Author: User

People who have used code layouts may feel that it is tedious to set a frame to a control. Recently, on GitHub, there is a UIView classification uiview-positioning that provides properties such as left, right, top, bottom, CenterX, centery, and so on, which are used in the layout. , will be more simple and convenient, the following describes the specific use.

Uiview-positioning's GitHub Address: https://github.com/freak4pc/UIView-Positioning, will uiview+positioning.h and uiview+ Copy the positioning.m file into the project.

When using the code layout, I am generally used to follow the three steps below.

1. Declare the control variable.

@implementation loginview{    UILabel *_usernamelabel;    Uitextfield *_usernamefield;}  

  2. In the initWithFrame method, create the control and set some of its basic properties, and then add it to the view's child view.

-(instancetype) initWithFrame: (CGRect) frame{if (self =[Super Initwithframe:frame]) {_usernamelabel = [UILabelNew]; _usernamelabel.font = [Uifont systemfontofsize:14.0 [Uicolor Blackcolor]; _usernamelabel.backgroundcolor =< Span style= "color: #000000;" > [Uicolor Clearcolor]; _usernamelabel.text = @ " user name:  "; [Self Addsubview:_usernamelabel]; _usernamefield = [Uitextfield new14.0 [Uicolor Blackcolor]; _usernamefield.borderstyle = Uitextborderstyleroundedrect; [Self Addsubview:_usernamefield]; } return self;}       

  3, in the Layoutsubviews method in the face of the layout of the control, the following use uiview-positioning classification of size, left, top, bottom, centery and other properties, by using the right property, you can take the right side Label control's Origin.x+size.width, and then add a padding value, you can get the origin.x of the TextField control on the right. We may often encounter, to set the two different height of the control, the vertical alignment, I deliberately set the height of the two controls are not the same, by setting their CenterY property to equal, you can keep the two controls in the vertical alignment.

-(void) layoutsubviews{    [Super layoutsubviews];        5;        _usernamelabel.size = Cgsizemake (n); _usernamelabel.left = margin; _usernamelabel.top = margin; _ Usernamefield.size = Cgsizemake (+); _usernamefield.left = _usernamelabel.right + padding; _ Usernamefield.centery = _usernamelabel.centery;}           

  Uiview-positioning by extending the UIView some of the properties, for the code layout or bring a lot of convenience, we recommend you can use it.

Using uiview-positioning for iOS development simplifies page layout

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.