@interface ViewController ()
{
NSLayoutConstraint *yellowViewTopConstraint;
NSLayoutConstraint *blueViewLeadConstraint; }
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UILayoutGuide *view_Guide = self.view.layoutMarginsGuide; /**
左边黄色view
*/
UIView *yellow_View = [[UIView alloc]init];
yellow_View.backgroundColor = [UIColor yellowColor];
yellow_View.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:yellow_View];
//左边距约束
NSLayoutConstraint *yellowView_Leading = [yellow_View.leadingAnchor constraintEqualToAnchor:view_Guide.leadingAnchor constant:];
//顶部约束
NSLayoutConstraint *yellowView_Top = [yellow_View.topAnchor constraintEqualToAnchor:view_Guide.topAnchor constant:];
//宽度约束
NSLayoutConstraint *yellowView_Width = [yellow_View.widthAnchor constraintEqualToConstant:];
//高度约束
NSLayoutConstraint *yellow_Height = [yellow_View.heightAnchor constraintEqualToConstant:];
[NSLayoutConstraint activateConstraints:@[yellowView_Leading,yellowView_Top,yellow_Height,yellowView_Width]]; yellowViewTopConstraint = yellowView_Top; /**
居中的红色view
*/
UIView *middleView = [[UIView alloc]init];
middleView.backgroundColor = [UIColor redColor];
middleView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:middleView];
//水平居中
NSLayoutConstraint *middleView_CenterX = [middleView.centerXAnchor constraintEqualToAnchor:view_Guide.centerXAnchor];
//垂直居中
NSLayoutConstraint *middleView_CenterY = [middleView.centerYAnchor constraintEqualToAnchor:view_Guide.centerYAnchor];
//宽度约束
NSLayoutConstraint *middleView_Width = [middleView.widthAnchor constraintEqualToConstant:];
//高度约束
NSLayoutConstraint *middleView_Height = [middleView.heightAnchor constraintEqualToConstant:];
[NSLayoutConstraint activateConstraints:@[middleView_CenterX,middleView_CenterY,middleView_Height,middleView_Width]]; /**
* 创建一个与黄色view相聚10的蓝色view
*/
UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView];
//左边约束
NSLayoutConstraint *blueView_Leading = [blueView.leadingAnchor constraintEqualToAnchor:yellow_View.trailingAnchor constant:];
//顶部约束于黄色view顶部对齐
NSLayoutConstraint *blueView_Top = [blueView.topAnchor constraintEqualToAnchor:yellow_View.topAnchor];
//宽度约束
NSLayoutConstraint *blueView_Width = [blueView.widthAnchor constraintEqualToConstant:];
//高度约束
NSLayoutConstraint *blueView_Height = [blueView.heightAnchor constraintEqualToConstant:];
[NSLayoutConstraint activateConstraints:@[blueView_Leading,blueView_Top,blueView_Width,blueView_Height]]; blueViewLeadConstraint = blueView_Leading; //创建一个执行动画按钮 UIButton *btnAnimate = [UIButton buttonWithType:UIButtonTypeCustom];
[btnAnimate setBackgroundColor:[UIColor redColor]];
[btnAnimate setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnAnimate setTitle:@"执行动画" forState:UIControlStateNormal];
[btnAnimate addTarget:self action:@selector(btnAnimateClick:) forControlEvents:UIControlEventTouchUpInside];
btnAnimate.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:btnAnimate];
//水平居中
NSLayoutConstraint *btnAnimate_CenterX = [btnAnimate.centerXAnchor constraintEqualToAnchor:view_Guide.centerXAnchor];
//底部约束
NSLayoutConstraint *btnAnimate_Bottom = [btnAnimate.bottomAnchor constraintEqualToAnchor:view_Guide.bottomAnchor constant:-]; //宽度约束
NSLayoutConstraint *btnAnimate_Width = [btnAnimate.widthAnchor constraintEqualToConstant:];
//高度约束
NSLayoutConstraint *btnAnimate_Height = [btnAnimate.heightAnchor constraintEqualToConstant:]; [NSLayoutConstraint activateConstraints:@[btnAnimate_Height,btnAnimate_Width,btnAnimate_CenterX,btnAnimate_Bottom]]; } - (void)btnAnimateClick:(UIButton *)sender
{ [UIView animateKeyframesWithDuration: delay: options:UIViewKeyframeAnimationOptionRepeat animations:^{ yellowViewTopConstraint.constant += ;
blueViewLeadConstraint.constant += ;
[self.view layoutIfNeeded]; } completion:^(BOOL finished) { }]; }

ios9-NSLayoutAnchor和UILayoutGuide实现自动布局的更多相关文章

  1. iOS9自动布局神器StackView

    http://www.jianshu.com/p/767f72b7d754 这篇文章紧跟上边autolayout的一些小技巧,如果你没有看过,不防先看下<你真的会用autolayout吗?> ...

  2. What's New in iOS9 iOS9功能改进

    What's New in iOS9 This article summarizes the key developer-related features introduced in iOS 9, w ...

  3. iOS9 适配

    iOS适配的相关内容的整理 之前iOS开发者一直很庆幸自己不用像安卓开发者那样适配各种不同类型的机型,但如今随着iPhone各种机型的改变,适配也成了我们开发中必须会的内容了.首先我们来了解一下对于不 ...

  4. iOS9新特性——堆叠视图UIStackView

    一.引言 随着autolayout的推广开来,更多的app开始使用自动布局的方式来构建自己的UI系统,autolayout配合storyBoard和一些第三方的框架,对于创建约束来说,已经十分方便,但 ...

  5. IOS9适配 MARK

    最近做了iOS 9的适配,程序出现大量警告也做了些处理,写出来分先给大家. 一.iOS 9适配 问题一: <Error>: CGContextSaveGState: invalid con ...

  6. iOS9适配+warning消除

    最近做了iOS 9的适配,程序出现大量警告也做了些处理,写出来分先给大家. 一.iOS 9适配 问题一: <Error>: CGContextSaveGState: invalid con ...

  7. iOS9新特性-UIStackView

    1. UIStackView相关属性理解 UIStackView是iOS9之后推出的,我也是第一次接触,在学习的过程中对于其中的相关属性,尤其是对其中的distribution几个属性值,一知半解的, ...

  8. iOS12、iOS11、iOS10、iOS9常见适配

    作者:花丶满楼 链接:https://juejin.im/post/5c49a7d0518825254e4d46fc 一.iOS12(Xcode10) 1.1.升级Xcode10后项目报错 不允许多个 ...

  9. iOS9支付宝无法调起客户端

    1.为了适配 iOS9.0 中的 App Transport Security(ATS)对 http 的限制,这里需要对 支付宝的请求地址 alipay.com 做例外,在 app 对应的 info. ...

随机推荐

  1. UVa - 12450 - SpaceRecon Tournament

    先上题目: Problem G: SpaceRecon Tournament SpaceRecon, the hottest game of 2011, is a real-time strategy ...

  2. Ubuntu安装vnc 解决乱码

    https://blog.csdn.net/dddxxxx/article/details/53580789 https://www.centos.bz/2017/12/%E8%A7%A3%E5%86 ...

  3. zoj——1202 Divide and Count

    Divide and Count Time Limit: 2 Seconds      Memory Limit: 65536 KB Jack has several beautiful diamon ...

  4. C++模板的特化与偏特化

    http://cppblog.com/SmartPtr/archive/2007/07/04/27496.html (1) 类模板定义一个栈的类模板,它可以用来容纳不同的数据类型 template & ...

  5. HTTP状态码图示

    这里总结下我们日常开发中常用的HTTP状态码,分享一个老外对HTTP状态码形象化用图片表示的网站:https://http.cat/ 总结如下: 表示服务器已经接收到了请求头,并且客户端应该继续发送请 ...

  6. 改动grub默认启动顺序

    grub如今有两个版本号,一个grub,一个grub2,两个版本号的操作不太一样. 装centos的朋友非常有可能是grub.我电脑装的是ubuntu14.04,为grub2.我演示grub2的过程. ...

  7. (WIP) DPDK理论学习(by quqi99)

    作者:张华  发表于:2016-04-22版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) 组成模 ...

  8. VIM学习笔记 比较文件(diff)

    比较 可以从命令行调用以下命令,来打开两个文件进行比较: vim -d file1 file2 如果已经打开了文件file1,那么可以在Vim中用以下命令,再打开另一个文件file2进行比较: :di ...

  9. Android资源之图像资源(图像级别资源)

    图像状态资源仅仅能定义有限的几种状态. 假设须要很多其它的状态,就要使用图像级别资源. 在该资源文件里能够定义随意多个图像级别. 每一个图像级别是一个整数区间,能够通过ImageView.setIma ...

  10. [DB][MySql]关于取得自增字段的值、及@@IDENTITY 与并发性问题

    对于刚从Oracle转向MySql的人都会为,MySql中没有Oracle里的Sequence而感到困惑.MySql中没有了Sequence,那么MySql的主键用什么方式来实现最好呢? 主要有下面几 ...