最终效果图如下:

很多限制条件都已经应用到了视图中,我们在解释一下:

·在我们的视图控制器的主视图中有两个灰色的视图。两个视图距视图控制器的视图左

边和右边都有一个标准的空白距离。视图的顶部距顶部的视图的顶部必须有一个标准的空白

距离。在两个灰色视图之间要有一个标准的垂直空白距离。

·在两个灰色视图里的垂直中央都要有一个按钮。

·在上面的灰色视图中的按钮距其父视图的左边要有一个标准的空白距离。

·在下面的灰色视图中的按钮的左边界应该和上面的灰色视图中的按钮的左边界对齐。

这就是交叉视图约束条件,这对我们来说是很重要的。

·灰色视图应该根据视图控制器方向的改变而进行从新调整大小。 ·两个灰色视图的高度必须是 100 像素。

//
// ThirdViewController.m
// AutoLayoutDemo
//
// Created by wildcat on 14-4-22.
// Copyright (c) 2014年 com.wildcat. All rights reserved.
// #import "ThirdViewController.h" @interface ThirdViewController ()
@property (nonatomic, strong) UIView *topGrayView;
@property (nonatomic, strong) UIButton *topButton;
@property (nonatomic, strong) UIView *bottomGrayView;
@property (nonatomic, strong) UIButton *bottomButton;
@end @implementation ThirdViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad{
[super viewDidLoad];
[self createGrayViews];
[self createButtons];
[self applyConstraintsToTopGrayView];
[self applyConstraintsToButtonOnTopGrayView];
[self applyConstraintsToBottomGrayView];
[self applyConstraintsToButtonOnBottomGrayView];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark 定义创建灰色视图函数
- (UIView *) newGrayView{
UIView *result = [[UIView alloc] init];
result.backgroundColor = [UIColor lightGrayColor];
result.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:result];
return result;
}
//视图初始化
- (void) createGrayViews{
self.topGrayView = [self newGrayView];
self.bottomGrayView = [self newGrayView];
}
#pragma mark 创建按钮
- (UIButton *) newButtonPlacedOnView:(UIView *)paramView{
UIButton *result = [UIButton buttonWithType:UIButtonTypeRoundedRect];
result.translatesAutoresizingMaskIntoConstraints = NO;
[result setTitle:@"Button" forState:UIControlStateNormal];
[paramView addSubview:result];
return result;
}
//添加到视图中
- (void) createButtons{
self.topButton = [self newButtonPlacedOnView:self.topGrayView];
self.bottomButton = [self newButtonPlacedOnView:self.bottomGrayView];
}
#pragma mark - 添加约束
#pragma mark 给上边的视图添加约束
- (void) applyConstraintsToTopGrayView{
NSDictionary *views = NSDictionaryOfVariableBindings(_topGrayView);
NSMutableArray *constraints = [[NSMutableArray alloc] init];
NSString *const kHConstraint = @"H:|-[_topGrayView]-|";//定义水平约束
NSString *const kVConstraint = @"V:|-[_topGrayView(==100)]";//定义垂直约束
/* Horizontal constraint(s) */
[constraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:kHConstraint
options:0
metrics:nil
views:views]];
/* Vertical constraint(s) */
[constraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:kVConstraint
options:0
metrics:nil
views:views]];
[self.topGrayView.superview addConstraints:constraints];
}
//给上边的按钮添加约束
- (void) applyConstraintsToButtonOnTopGrayView{
NSDictionary *views = NSDictionaryOfVariableBindings(_topButton);
NSMutableArray *constraints = [[NSMutableArray alloc] init];
NSString *const kHConstraint = @"H:|-[_topButton]";
/* Horizontal constraint(s) */
[constraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:kHConstraint options:0
metrics:nil
views:views]
];
/* Vertical constraint(s) */
[constraints addObject:
[NSLayoutConstraint constraintWithItem:self.topButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.topGrayView
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f]];
[self.topButton.superview addConstraints:constraints];
}
#pragma mark 给底部的视图添加约束
- (void) applyConstraintsToBottomGrayView{
NSDictionary *views = NSDictionaryOfVariableBindings(_topGrayView, _bottomGrayView);
NSMutableArray *constraints = [[NSMutableArray alloc] init];
NSString *const kHConstraint = @"H:|-[_bottomGrayView]-|";
NSString *const kVConstraint = @"V:|-[_topGrayView]-[_bottomGrayView(==100)]";
/* Horizontal constraint(s) */
[constraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:kHConstraint options:0
metrics:nil
views:views]
];
/* Vertical constraint(s) */
[constraints addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:kVConstraint options:0
metrics:nil
views:views]
];
[self.bottomGrayView.superview addConstraints:constraints];
}
//给底部的按钮添加约束
-(void) applyConstraintsToButtonOnBottomGrayView{
NSDictionary *views = NSDictionaryOfVariableBindings(_topButton, _bottomButton);
NSString *const kHConstraint = @"H:[_topButton][_bottomButton]";
/* Horizontal constraint(s) */
[self.bottomGrayView.superview addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:kHConstraint
options:0
metrics:nil
views:views]];
/* Vertical constraint(s) */
[self.bottomButton.superview addConstraint:
[NSLayoutConstraint constraintWithItem:self.bottomButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual
toItem:self.bottomGrayView
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f]
];
}
- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
} @end

IOS使用不同父类的 view 进行约束的更多相关文章

  1. day-25-类的继承顺序-父类对子类的约束-多态-队列和栈

    一.类的继承顺序 只要继承object类就是新式类 不继承object类的都是经典类 在python3 中所有的类都继承object类,都是新式类 在python2 中不继承object的类都是经典类 ...

  2. iOS 新建xib文件时,最外层view的约束问题

    今天用在利用xib实例化view 时, 生成的view的自动布局总是用问题.具体来说,宽和高都不能和父view正确变化.仔细检查,发现下图: 注意这里右上角的Autoresizing部分,并没有设置正 ...

  3. iOS: 学习笔记, 添加一个带界面约束的控制器

    1. 创建一个空iOS应用程序(Empty Application). 2. 添加加控制器类. 修改控制器类的viewDidLoad - (void)viewDidLoad { [super view ...

  4. iOS: 学习笔记, 加入一个带界面约束的控制器

    1. 创建一个空iOS应用程序(Empty Application). 2. 加入加控制器类. 改动控制器类的viewDidLoad - (void)viewDidLoad { [super view ...

  5. 为view添加约束constraints

    在相应要设置约束的view中按住鼠标右键进行拖拽,然后向指定的方向添加约束,如图: 拖拽的时候会显示一条蓝线,如上图所示,然后手指离开鼠标的时候会弹出向对应的约束供添加约束的时候进行使用如图:

  6. iOS 使用xib定义一个View,修改frame无效问题解决

    遇到过好多次使用自定义view,修改frame无效问题, 之前都是放弃xib,直接手写,发现手写简单的还行,复杂的UI就坑逼了.所以还是需要用到可视化编辑的xib. 整理一下,自己备忘也供iOS开发的 ...

  7. 全面理解iOS开发中的Scroll View[转]

    from:http://mobile.51cto.com/hot-430409.htm 可能你很难相信,UIScrollView和一个标准的UIView差异并不大,scroll view确实会多一些方 ...

  8. iOS边练边学--view的封装

    一.view封装的思路: *如果一个view内部的子控件比较多,一般会考虑自定义一个view,把它内部的子控件的创建屏蔽起来,不让外界关心 *外界可以传入对应的模型数据给view,view拿到模型数据 ...

  9. iOS: Assertion failure on picker view

    Q:I'm getting an assertion failure while scrolling a picker view w/ zero data(zero rows). While scro ...

随机推荐

  1. C++ Primer 学习笔记_54_类和数据抽象 --拷贝构造函数、赋值运算符

    拷贝控制 --复制构造函数.赋值操作符 引言: 当定义一个新类型时,须要显式或隐式地指定复制.赋值和撤销该类型的对象时会发生什么– 复制构造函数.赋值操作符和析构函数的作用!      复制构造函数: ...

  2. 私人定制javascript事件处理机制(浅谈)

    看到园子里关于事件监听发表的文章,我都有点不好意思写了.不过想想我的题目以私人定制作开头也就妥妥地写吧. 事件相关概念 1.事件类型 发生事件的字符串 有传统事件类型 比如表单.window事件等 D ...

  3. CSharp设计模式读书笔记(19):备忘录模式(学习难度:★★☆☆☆,使用频率:★★☆☆☆)

    备忘录模式(Memento Pattern):在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态.它是一种对象行为型模式,其别名为Tok ...

  4. swift 注意事项 (十六) —— 可选链

    可选链(Optional Chaining) 我们都知道"可选型"是什么.那么可选链又是什么,举个样例解释一下: struct MyName{      var name } st ...

  5. [ 夜间模式 ] NightVersion

    DKNightVersion框架.重写管理类 & 控件的分类!--可重写{ 使用GCD.runtime.delegate等 & 工具类的创建 } ================ 1. ...

  6. Linux基础正则表达式:grep,sed

    先说明语系对正则表达式的影响    LANG=C:0,1,2,3,4...A,B,C,D...Z a b c d ... z    LANG=zh_CN:0,1,2,3,4...a A b B c C ...

  7. 我在Github上的flare-spark项目

    Flare-Spark 介绍 我在自己的github上建了个flare-spark项目,本身是Apache Spark项目Master分支的镜像.在Spark的基础上,添加了flare子项目. 估计大 ...

  8. 搭建一个三台服务器的Memcached集群

    关于memcached的基础知识可以查看博客其他博文,这里只记录了搭建的过程,谢谢! 1.分别在三台服务器上安装Memcached并启动 第一.由于memcached是基于libevent的事件处理, ...

  9. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

  10. UiAutomator源码分析之注入事件

    上一篇文章<UiAutomator源码分析之UiAutomatorBridge框架>中我们把UiAutomatorBridge以及它相关的类进行的描述,往下我们会尝试根据两个实例将这些类给 ...