iOS在纯代码中使用AutoLayout添加约束

本文介绍了在iOS开发中如何使用纯代码的AutoLayout来添加约束,强调了设置`translatesAutoresizingMaskIntoConstraints`为`NO`的重要性,并通过示例代码展示了VFL(Visual Format Language)的使用方法,包括水平和垂直方向的约束设置,以及如何利用metrics参数。
摘要由CSDN通过智能技术生成
 

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值