@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. 什么是js严格模式?

    [03] 严格模式 ECMAScript 5 引入了严格模式(strict mode)的概念.严格模式是为JavaScript 定义了一种不同的解析与执行模型.在严格模式下,ECMAScript 3  ...

  2. python整数转ASCII码

    # *-* coding:utf-8 *-* import binascii data = [1441465642, 251096121, -870437532, -944322827, 647240 ...

  3. noip模拟赛 蒜头君的排序

    分析:其实就是求m个区间的逆序对个数,题目真的是明摆着让我们用莫队算法,套用树状数组就可以了. 具体怎么转移呢?如果移动R,那么对区间[l,r]有影响的是R左边的元素,我们只需要看有多少在R左边比a[ ...

  4. 这个贴子的内容值得好好学习--实例详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化

    感觉要DJANGO用得好,ORM必须要学好,不管理是内置的,还是第三方的ORM. 最最后还是要到SQL.....:( 这一关,慢慢练啦.. 实例详解Django的 select_related 和 p ...

  5. php 生成订单号

    最近在练手一个订单提交的小项目,需要用到生成订单号,网上找了下,觉得这个最好. function build_order_no(){ return date('Ymd').substr(implode ...

  6. [学习笔记]JavaScript基础

    JavaScript概述 1. JavaScript定义 JavaScript是Netscape公司开发的一种基于对象和事件驱动的脚本语言.它是弱类型语言.仅仅能由浏览器解释运行. 当中: 脚本语言: ...

  7. android传感器;摇一摇抽签功能

    package com.kane.sensortest; import java.util.Random; import android.hardware.Sensor; import android ...

  8. Matlab得到二值图像中最大连通区域

    有时候要将二值化图像中最大的连通域保存下来.以下函数提供了一种方法: %function [img]=maxLianTongYu(I):求图像中最大的连通域 %输入:I 输入图像 %输出:img 仅包 ...

  9. hdu 1728 逃离迷宫 bfs记步数

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  10. Linux Framebuffer驱动剖析之中的一个—软件需求

    嵌入式企鹅圈将以本文作为2015年的终结篇,以回应第一篇<Linux字符设备驱动剖析>.嵌入式企鹅圈一直专注于嵌入式Linux和物联网IOT双方面的原创技术分享,稍后会公布嵌入式企鹅圈的2 ...