最终效果图如下:

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

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

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

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

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

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

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

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

·灰色视图应该根据视图控制器方向的改变而进行从新调整大小。 ·两个灰色视图的高度必须是 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. ASP.NET MVC上传文件----uploadify的使用

    课程设计需要实现上传文件模块,本来ASP.NET是有内置的控件,但是ASP.NET MVC没有,所以就有两种方法:自定义和采用第三方插件.由于时间的关系,故采用第三方插件:uploadify. upl ...

  2. MVC中实现多按钮提交(转)

    有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...

  3. ReportNG测试报告模板定制

      部分参考:http://tech.it168.com/a2013/0906/1530/000001530755_3.shtml ReportNG提供了简单的方式来查看测试结果,并能对结果进行着色, ...

  4. SpringMVC4 + Spring + MyBatis3

    SpringMVC4 + Spring + MyBatis3 本文使用最新版本(4.1.5)的springmvc+spring+mybatis,采用最间的配置方式来进行搭建. 1. web.xml 我 ...

  5. Tomcat剖析(一):一个简单的Web服务器

    Tomcat剖析(一):一个简单的Web服务器 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三):连接器 ...

  6. javascript中类的属性研究

    原文:javascript中类的属性研究 本篇文章主要针对javascript的属性进行分析,由于javascript是一种基于对象的语言,本身没有类的概念,所以对于javascript的类的定义有很 ...

  7. firefox里面title乱码

    原文:firefox里面title乱码 昨天 在notepad++里面写得文档里面title里面有中文,即使在文档里面写有charset=’UTF-8’, 但是保存后在firefox运行,浏览器标签标 ...

  8. 导出DBF,并且提供下载 .

    原文:导出DBF,并且提供下载 . 导出DBF,并且提供下载 #region Declare string mFilePath = MapPath("../DataTmp/");  ...

  9. 由一个LED闪烁问题发现的MTK的LED driver中存在的问题

    今天依据最新的需求要对LED灯的提示闪烁频率进行改动,将之前默认的2000ms改为10000ms,可是改动之后没有产生预料中的效果,而是变成了常量,百思不得其解,最后还是read the fuckin ...

  10. 记第五届山东省ACM程序设计比赛——遗憾并非遗憾

    记第五届山东省ACM程序设计比赛 5月10日上午9点半左右,我们的队伍从学校出发,一个多小时后到达本次比赛的地点-哈尔滨工业大学. 报道,领材料,吃午饭,在哈工大的校园里逛了逛,去主楼的自习室歇息了一 ...