IOS6之自动布局

IOS6之AutoLayout(一)

http://blog.csdn.net/zfpp25_/article/details/8861221

IOS6之AutoLayout(二)

http://blog.csdn.net/zfpp25_/article/details/8861855

IOS6之AutoLayout(三)

http://blog.csdn.net/zfpp25_/article/details/8861958

IOS6之AutoLayout(四)

http://blog.csdn.net/zfpp25_/article/details/8862040

IOS6之AutoLayout(五)

http://blog.csdn.net/zfpp25_/article/details/8862157


IOS6出现之后,新建一个ViewController,从NIB文件初始化,然后添加到window上。然后用5.0模拟器去执行项目,会发现app崩溃了。原因就是IOS6之后,NIB文件的选项中多了AutoLayout属性,而IOS6之前是不支持的,所以项目崩溃了,解决办法是取消AutoLayout就可以在5的模拟器上运行了。但一味的避开AutoLayout这个新特性也不是办法,所以研究了下。


AutoLayout是什么?

Before iOS6, layout was mainly by autosizing, also known as the “springs and struts” method. In iOS6, we will use AutoLayout, which will make it easier to design interfaces for various sceen sizes.

简单的理解就是为了实现屏幕中控件的布局,android开发时候,布局是件很痛苦的事情,因为,android的屏幕型号太多了。幸好IOS到目前为止比android程序员在屏幕适配上轻松许多。


为什么要使用AutoLayout?

IOS设备的尺寸会渐渐的更新,所以早点适应相对布局来完成UI界面是必要的。


如何使用AutoLayout?


下面会详细的讲解:

例1:我们以前创建一个按钮,按钮的frame为 CGRect(20, 30, 280, 44) 的按钮,我们会毫不犹豫的这样写:

  1. UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];  
  2. [firstButtonsetTitle:@"FirstFirstFirstFirst"forState:UIControlStateNormal];  
  3. firstButton.frame = CGRect(20, 30, 280, 49);  
  4. [self.viewaddSubview:firstButton];  

这样很简单的就实现了按钮在self.view中的的绝对布局。


下面来看看,使用AutoLayout相对布局,同样的一个按钮是怎么实现的:

  1. UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  2. [firstButtonsetTitle:@"FirstFirstFirstFirst"forState:UIControlStateNormal];  
  3. [firstButton sizeToFit];  
  4. firstButton.translatesAutoresizingMaskIntoConstraints =NO;  
  5. [self.viewaddSubview:firstButton];  

//开始相对布局

//首先,先对其按钮的左边和self.view左边的距离

   

  1. NSLayoutConstraint *constraint = [NSLayoutConstraint  
  2.                                      constraintWithItem:firstButton  
  3.                                      attribute:NSLayoutAttributeLeading  
  4.                                      relatedBy:NSLayoutRelationEqual  
  5.                                      toItem:self.view  
  6.                                      attribute:NSLayoutAttributeLeading  
  7.                                      multiplier:1.0f  
  8.                                      constant:20.f];  
  9.    [self.view addConstraint:constraint];  

    //然后,先对其按钮的右边和self.view右边的距离

  1. constraint = [NSLayoutConstraint  
  2.               constraintWithItem:firstButton  
  3.               attribute:NSLayoutAttributeTrailing  
  4.               relatedBy:NSLayoutRelationEqual  
  5.               toItem:self.view  
  6.               attribute:NSLayoutAttributeTrailing  
  7.               multiplier:1.0f  
  8.               constant:-20.f];  
  9. [self.view addConstraint:constraint];  

//然后,先对其按钮的上边和self.view上边的距离

    

  1. constraint = [NSLayoutConstraint  
  2.                   constraintWithItem:firstButton  
  3.                   attribute:NSLayoutAttributeTop  
  4.                   relatedBy:NSLayoutRelationEqual  
  5.                   toItem:self.view  
  6.                   attribute:NSLayoutAttributeTop  
  7.                   multiplier:1.0f  
  8.                   constant:30.f];  
  9.     [self.view addConstraint:constraint];  

    //最后,先对其按钮的下边和self.view下的距离

    

  1. constraint = [NSLayoutConstraint  
  2.                   constraintWithItem:firstButton  
  3.                   attribute:NSLayoutAttributeBottom  
  4.                   relatedBy:NSLayoutRelationEqual  
  5.                   toItem:self.view  
  6.                   attribute:NSLayoutAttributeBottom  
  7.                   multiplier:1.0f  
  8.                   constant:((30 +44) - (460-44))];  
  9.  [self.view addConstraint:constraint];  



同样也实现了一个按钮,但代码量实在是翻了几倍,现在可以理解android程序员的痛苦了。


那么上面相对布局的代码中,一些关键点如何理解呢?下面做简单介绍:

0、translatesAutoresizingMaskIntoConstraints:

The translatesAutoresizingMaskIntoConstraints property is set to NO, so our constraints will not conflict with the old “springs and struts” method.

1、NSLayoutConstraint类,是IOS6引入的,字面意思是“约束”、“限制”的意思,实现相对布局,就依靠这个类了;

2、怎么理解这个方法调用:

  1. NSLayoutConstraint *constraint = [NSLayoutConstraint  
  2.                                    constraintWithItem:firstButton        firstButton是我们实例化的按钮  
  3.                                    attribute:NSLayoutAttributeLeading    firstButton的左边  
  4.                                    relatedBy:NSLayoutRelationEqual       firstButton的左边与self.view的左边的相对关系  
  5.                                    toItem:self.view                      指定firstButton的相对的对象是self.view   
  6.                                    attribute:NSLayoutAttributeLeading    相对与self.view的左边(NSLayoutAttributeLeading是左边的意思)  
  7.                                    multiplier:1.0f                                       (后文介绍)  
  8.                                    constant:20.f];                       firstButton左边相对self.view左边,偏移了20.0f  
  9.     [self.view addConstraint:constraint];                                将这个约束添加到self.view上  

3、其他部分代码同理,这样就不难理解了:

NSLayoutAttributeTrailing  右边

NSLayoutAttributeTop    上边

NSLayoutAttributeBottom  下边

4、这个难理解吗?constant:((30 + 44) - (460-49))];  

按钮上沿坐标距selfview的上沿是30,打算让按钮的高度为44, 屏幕的总高度为460,再去掉tabBar的高度49。那按钮的下沿距离self.view的下沿的距离是多少 ?


IOS相对布局应该可以理解了,后续会继续更新~


转载请保留,原文链接:http://write.blog.csdn.net/postedit/8861221

若发现有不合适或错误之处,还请批评指正,不胜感激。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值