iOS autolayout使用

随着苹果机型越来越多,多机型适配成了一个问题,有的计算屏幕大小 ,有的用autoresize,个人感觉最好用的还是苹果提供的autolayout.当然,每个人的喜好不一样,这就要看自已的喜好了。autolayout实现多机型适配主是用用到约束,通过约束能够定位控件的位置,前提是父视图的大小确定。

下面从最简单的开始说起,在视图中添加Button,父视图占整个屏幕大小是可以确定的

竖屏                                                                               横屏

 
                       

下面看看代码的实现:如下

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIButton *TopLeftBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [TopLeftBtn setTitle:@"TopLeftBtn" forState:UIControlStateNormal];
    [TopLeftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [TopLeftBtn setBackgroundColor:[UIColor greenColor]];
   //关闭autoresize
    [TopLeftBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:TopLeftBtn];
    //前约束  也叫左约束   离父视图self.view的左边间距为10像素
    NSLayoutConstraint *topLeftBtnleadingConstraint=[NSLayoutConstraint constraintWithItem:TopLeftBtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:10];
    [self.view addConstraint:topLeftBtnleadingConstraint];
    //上约束   离父视图self.view的上边间距为10像素
    NSLayoutConstraint *topLeftBtntopConstraint=[NSLayoutConstraint constraintWithItem:TopLeftBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:10];
    [self.view addConstraint:topLeftBtntopConstraint];

    //右约束  离父视图self.view的右边间距为10像素
    NSLayoutConstraint *topLeftBtnRightConstraint=[NSLayoutConstraint constraintWithItem:TopLeftBtn attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:-10];
    [self.view addConstraint:topLeftBtnRightConstraint];

    //下约束  离父视图self.view的下边间距为10像素
    NSLayoutConstraint *topRightBtnBottomConstraint=[NSLayoutConstraint constraintWithItem:TopLeftBtn attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:-10];
    [self.view addConstraint:topRightBtnBottomConstraint];
}

添加上面4个约束就可以确定Button的大小了,好了就这么简单,这是一个简单的,如果来点复杂的呢,那好就看下面的了

竖屏                                                                                横屏

 
                       

上面两个按钮,下面一个按钮,上面两按钮宽度一样,所有Button高度一样,所有间距都是10像素,下面上代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIButton *TopLeftBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [TopLeftBtn setTitle:@"TopLeftBtn" forState:UIControlStateNormal];
    [TopLeftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [TopLeftBtn setBackgroundColor:[UIColor greenColor]];
    [TopLeftBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:TopLeftBtn];

    UIButton *TopRightBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [TopRightBtn setTitle:@"TopRightBtn" forState:UIControlStateNormal];
    [TopRightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [TopRightBtn setBackgroundColor:[UIColor redColor]];
    [TopRightBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:TopRightBtn];

    UIButton *BottomBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [BottomBtn setTitle:@"BottomBtn" forState:UIControlStateNormal];
    [BottomBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [BottomBtn setBackgroundColor:[UIColor blueColor]];
    [BottomBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:BottomBtn];

    //TopLeftBtn相对于self.view的左约束  距父视图self.view的左间距是10像素
    NSLayoutConstraint *topLeftBtnleadingConstraint=[NSLayoutConstraint constraintWithItem:TopLeftBtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:10];
    [self.view addConstraint:topLeftBtnleadingConstraint];

    //TopLeftBtn相对于self.view的上约束   距父视图self.view的上间距是 10像素
    NSLayoutConstraint *topLeftBtntopConstraint=[NSLayoutConstraint constraintWithItem:TopLeftBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:10];
    [self.view addConstraint:topLeftBtntopConstraint];

    //TopRightBtn相对于TopLeftBtn的约束 TopRightBtn与TopLeftBtn的上平齐
    NSLayoutConstraint *topRightBtnTopConstraint=[NSLayoutConstraint constraintWithItem:TopRightBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:TopLeftBtn attribute:NSLayoutAttributeTop multiplier:1 constant:0];
    [self.view addConstraint:topRightBtnTopConstraint];

    //TopRightBtn相对于TopLeftBtn的约束 TopRightBtn的左边与TopLeftBtn的右边相差10像素
    NSLayoutConstraint *topRightBtnLeadConstraint=[NSLayoutConstraint constraintWithItem:TopRightBtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:TopLeftBtn attribute:NSLayoutAttributeTrailing multiplier:1 constant:10];
    [self.view addConstraint:topRightBtnLeadConstraint];

    //TopRightBtn相对于self.view的约束 TopRightBtn的右边与父视图的右边间距是10像素
    NSLayoutConstraint *topRightBtnTrailConstraint=[NSLayoutConstraint constraintWithItem:TopRightBtn attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:-10.0];
    [self.view addConstraint:topRightBtnTrailConstraint];

    //TopRightBtn与TopLeftBtn宽度相等
    NSLayoutConstraint *topRightAndLeftBtnWidthEqualConstraint=[NSLayoutConstraint constraintWithItem:TopRightBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:TopLeftBtn attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    [self.view addConstraint:topRightAndLeftBtnWidthEqualConstraint];

    //TopRightBtn与TopLeftBtn高度相等
    NSLayoutConstraint *topRightAndLeftBtnHeightEqualConstraint=[NSLayoutConstraint constraintWithItem:TopRightBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:TopLeftBtn attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    [self.view addConstraint:topRightAndLeftBtnHeightEqualConstraint];

    //TopLeftBtn与BottomBtn左对齐
    NSLayoutConstraint *bottomBtnleadingConstraint=[NSLayoutConstraint constraintWithItem:TopLeftBtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:BottomBtn attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
    [self.view addConstraint:bottomBtnleadingConstraint];

    //TopRightBtn与BottomBtn左对齐
    NSLayoutConstraint *bottomBtnTrailConstraint=[NSLayoutConstraint constraintWithItem:TopRightBtn attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:BottomBtn attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
    [self.view addConstraint:bottomBtnTrailConstraint];

    //BottomBtn的上边距约束离TopLeftBtn的底部为10像素
    NSLayoutConstraint *bottomBtnTopConstraint=[NSLayoutConstraint constraintWithItem:BottomBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:TopLeftBtn attribute:NSLayoutAttributeBottom multiplier:1 constant:10];
    [self.view addConstraint:bottomBtnTopConstraint];

    //BottomBtn的上边距约束离self.view的底部是10像素
    NSLayoutConstraint *bottomBtnBottomConstraint=[NSLayoutConstraint constraintWithItem:BottomBtn attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:-10];
    [self.view addConstraint:bottomBtnBottomConstraint];

    //TopLeftBtn的高度与BottomBtn的高度相同
    NSLayoutConstraint *bottomBtnHeightConstraint=[NSLayoutConstraint constraintWithItem:TopLeftBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:BottomBtn attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    //multiplier百度是乘数  constant 常数

    [self.view addConstraint:bottomBtnHeightConstraint];

    //更新约束
//    [self.view setNeedsUpdateConstraints];
//
//    [self.view updateConstraintsIfNeeded];
}

就这么多是不是很方便,那我们现在来看哈约束是怎么实现定位的,如果手机是5s的话,像素就是640*1136 缩小一倍就是320*568

竖屏时:设三个Button的高度是x未知数,三个Button高度一样,10+x+10+x+10=568 --->x=269,三个按钮高度都是269像素。BottomBtn的宽度是10+y+10=320--->y=300,BottomBtn的宽就是300像素。现在主是计算TopLeftBtn  TopRightBtn的宽了,TopLeftBtn与TopRightBtn的宽相等,那么10+z+10+z+10=320-->z=145,他们的宽就是145像素了,现在一切就好说了

TopLeftBtn的frame 就是(10,10,145,269)   TopRightBtn的frame为(10+145+10,10,145,269)

BottomBtn的frame就是(10,10+269+10,145,269)

横屏一样,只不过是宽度换过来了,其他的一样。

如果是4s或者6  6plus只不过是屏幕尺寸不一样而已都按上面的公式计算,得出的frame也不一样,所以说只要你的约束加得好,就会控制控件的位置,实现多屏配适配。是不是觉得很好学,有些人会觉得代码写得太多了,确实这样一个一个加约束确实挺麻烦的,如果又想代码写得少,又想效果好,请听下回分解

时间: 2024-08-07 13:37:31

iOS autolayout使用的相关文章

从此爱上iOS Autolayout

转:从此爱上iOS Autolayout 这篇不是autolayout教程,只是autolayout动员文章和经验之谈,在本文第五节友情链接和推荐中,我将附上足够大家熟练使用autolayout的教程.这篇文章两个月前就想写下来,但因为一直工作较多,没有时间来完成.今天终于狠下心,丢下代码不写,来完成他吧! 一.别和我提Autolayout,我想死!! 从iOS6/xcode4开始,苹果开始提供了autolayout——一种对不同屏幕尺寸有更好兼容的自动布局机制,但我相信大多数人在刚接触auto

iOS autoLayout总结

本文转自 http://ruikq.github.io/ios/autolayout/uiscrollview/2015/01/27/iOS-autolayout%E6%80%BB%E7%BB%93.html autolayout, and uiscrollview 以前学习iOS的时候没怎么接触过autoLayout,自从iPhone6个6+出来之后一直在为以前的app做适配,所以使用了大量的autoLayout做适配,一开始很不习惯,但是越用越觉得好用,接触到现在遇到很多问题,在这里总结一下

iOS — Autolayout之Masonry解读

前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时代 window的size固定为(320,480) 我们只需要简单计算一下相对位置就好了 在iphone4-iphone4s时代 苹果推出了retina屏 但是给了码农们非常大的福利:window的size不变 在iphone5-iphone5s时代 window的size变了(320,568) 这时auto

IOS Autolayout

●  Autolayout是一种"自动布局"技术,专门用来布局UI界面的 ●  Autolayout自iOS 6开始引入,由于Xcode 4的不给力,当时并没有得到很大推 广 ●  自iOS 7(Xcode 5)开始,Autolayout的开发效率得到很大的提升 ●  苹果官方也推荐开发者尽量使用Autolayout来布局UI界面 ●  Autolayout能很轻松地解决屏幕适配的问题 Autoresizing ● 在Autolayout之前,有Autoresizing可以作屏幕适配,

iOS.AutoLayout.2.CustomViewWithAutoLayout

Custom View Which Support AutoLayout 创建支持AutoLayout的Custom View AutoLayout 通过使view更加的自组织来减轻controller类的负担. 当实现custom view类时,需要提供足够的信息来使AutoLayout系统能够正确计算和满足约束(Constraints). 1. 为Custom View指定 intrinsic size (内在的/固有的 size) "Leaf-level views, such as bu

IOS AutoLayout 代码实现约束—VFL

在autolayout下,尽管使用IB来拖放控件,但仍然避免不了用代码来创建控件,这是约束需要代码来实现. IOS 提供了两种添加约束的方法 第一种: +(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multip

IOS AutoLayout详解(三)用代码实现(附Demo下载)

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的IOS SDK专栏,这个专栏我会持续进行更新. IOS SDK详解 前言: 在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建就派上用场了,这篇文章我会详解用代码实现的两个主要函数,然后讲解一个Demo,最后Demo我会附上下载链接. 用代码实现的函数一 第一个函数通过描述两个view的参考线之间的约束来创建约束,例如有一个label和一个t

iOS AutoLayout自动布局中级开发教程(2)-初识autolayout

通过storyboard,我们初识一下autolayout的表现形式: 看下图,使用storyboard创建的两个控件视图: 上图中的四个圆角框内的距离值,就是约束; 比如上图的第二个视图,他的 约束是:  距离 view左边,右边界的距离,这样就确定了 宽度和水平方向上的位置,还有距离上面(第一个)视图的距离,还有高,这样就确定了 视图的高度和y轴的位置;这样就可以在一个二维空间(屏幕)中唯一的确定这个视图的位置了; 但是,需要注意的,在添加距离上一个视图下边界的约束时,第一个视图的位置一定要

iOS AutoLayout 及SizeClass 自动布局(一)

一.关于自动布局(Autolayout) 在Xcode中,自动布局看似是一个很复杂的系统,在真正使用它之前,我也是这么认为的,不过事实并非如此. 我们知道,一款iOS应用,其主要UI组件是由一个个相对独立的可视单元构成,这些可视单元有的主要负责向用户输出有用的信息,有些则负责信息的输入(交互),交互的过程中往往还伴随有动画的效果,已达到整个信息传递的连贯性以及用户体验的细腻感.可视单元,在实际开发中主要是view.button等,那么这些可视单元的关系由两个基本的关系构成:兄弟关系和父子关系,整

IOS AutoLayout详解(一)

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言: AutoLayout是让UI适应控件自适应设备尺寸变化的一项关键技术.随着IOS Device的尺寸越来越多,很难再像以前一样用一些固定的数字来布置UI. AutoLayout的实现有两种方式 Storyboard 代码 用Storyboard实现又有三种可选方式 蓝色参考线来让XCode自动创建 鼠标拖拽来实现 XCode中的一些选项按键配置 本文的内容包括 Storyboard上的一些基础知识 基于蓝色参考