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