@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. Tclientdate的排序

    CDS_common.IndexDefs.Clear;        CDS_common.AddIndex('JSPH','JSPH',[],'JSPH');        CDS_common.A ...

  2. Node+Express中请求和响应对象

    在用 Express 构建 Web 服务器时,大部分工作都是从请求对象开始,到响应对象终止. url的组成: 协议协议确定如何传输请求.我们主要是处理 http 和 https.其他常见的协议还有 f ...

  3. [luoguP1516] 青蛙的约会(扩展欧几里得)

    传送门 对于数论只会gcd的我,也要下定决心补数论了 列出方程 (x + t * m) % l = (y + t * n) % l 那么假设 这两个式子之间相差 num 个 l,即为 x + t * ...

  4. [luoguP2890] [USACO07OPEN]便宜的回文Cheapest Palindrome(DP)

    传送门 f[i][j] 表示区间 i 到 j 变为回文串所需最小费用 1.s[i] == s[j] f[i][j] = f[i + 1][j - 1] 2.s[i] != s[j] f[i][j] = ...

  5. Mysql 使用delete drop truncate 删除数据时受外键约束影响解决方案

    先禁用数据库的外键约束: set foreign_key_checks=0; 进行删除操作 delete.drop.truncate 恢复数据库外键约束: set foreign_key_checks ...

  6. C#: 根据指定压缩比率压缩图片

    直接上代码: /// <summary> /// 根据指定压缩比率压缩图片 /// </summary> /// <param name="original&q ...

  7. 使用jcaptcha插件生成验证码

    1.从官网http://jcaptcha.sourceforge.net/下载插件.将对应jar包导入到lib文件夹下 2.创建一个CaptchaServiceSingleton类用来获取jcaptc ...

  8. 微信公众号H5用户授权

    其实不是很难,总结起来就是: 1.微信公众号管理后台设置redirect_uri. 2.然后发起一个请求去重定向获取code,然后把获取到code之后重定向的URL放在获取code的URL中 3.获取 ...

  9. Asp.Net实现JS前台带箭头的流程图方法总结!(个人笔记,信息不全)

    Asp.Net实现JS前台带箭头的流程图方法总结!(持续更新中) 一.返回前台json格式 json5 = "[{\"Id\":2259,\"Name\&quo ...

  10. POJ 1300 Door Man(欧拉通路)

    题目描写叙述: 你是一座大庄园的管家. 庄园有非常多房间,编号为 0.1.2.3..... 你的主人是一个心不在 焉的人,常常沿着走廊任意地把房间的门打开.多年来,你掌握了一个诀窍:沿着一个通道,穿 ...