AutoLayout约束是苹果官方给定得约束信息。对于这个约束和UIView+AutoLayout.h延展的用法类似,不过加上了延展之后,不用写VFL语言
下面先看看以autolayout实现的效果为例我们讲解一下
下面开始上代码以用户名和密码为例,来说说,
UITextField * userNameTextFiled = [[UITextField alloc] init]; userNameTextFiled.borderStyle = UITextBorderStyleRoundedRect; userNameTextFiled.placeholder = @"用户名"; userNameTextFiled.translatesAutoresizingMaskIntoConstraints=NO; [self.view addSubview:userNameTextFiled]; UITextField * userPwdTextFiled = [[UITextField alloc] init]; userPwdTextFiled.borderStyle = UITextBorderStyleRoundedRect; userPwdTextFiled.placeholder = @"密码"; userPwdTextFiled.translatesAutoresizingMaskIntoConstraints=NO; [self.view addSubview:userPwdTextFiled];
在这里有两点必须说明:1:在对控件allo的时候我们一定忘掉设置他得CGRectMake
2:也是重中之重的,不然会导致严重的性能问题,一定要对当前控件的
translatesAutoresizingMaskIntoConstraints的属性设置为NO,不然无法使用。。。
首先看看这两个约束的方法
- (void)addConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided. Instead, set NSLayout
iOS在纯代码中使用AutoLayout添加约束
于 2015-11-28 11:31:31 首次发布
本文介绍了在iOS开发中如何使用纯代码的AutoLayout来添加约束,强调了设置`translatesAutoresizingMaskIntoConstraints`为`NO`的重要性,并通过示例代码展示了VFL(Visual Format Language)的使用方法,包括水平和垂直方向的约束设置,以及如何利用metrics参数。
摘要由CSDN通过智能技术生成