最终效果图如下:

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

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

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

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

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

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

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

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

·灰色视图应该根据视图控制器方向的改变而进行从新调整大小。 ·两个灰色视图的高度必须是 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实现FORM认证的一些使用技巧

    原文转发:http://www.cnblogs.com/Showshare/archive/2010/07/09/1772886.html 最近因为项目代码重构需要重新整理用户登录和权限控制的部分,现 ...

  2. JBPM——MyEclipse开发环境的搭建

    第一次接触JBPM我不知道如何在工程中的应用.查了一些资料.大约在JBPM随着时代的发展有一定的了解.首先JBPM它是JBoss件平台的一个组成部分.是一个灵活的,易扩展的工作流管理系统,仅仅只是这个 ...

  3. C#记录日志、获取枚举值 等通用函数列表

    )             {                 ] >=  && ipvals[] <=                  && ipval ...

  4. maven+hudson构建集成测试平台

     1.下载hudson.war.2.命令行运行:java -jar hudson.war --httpPort=8070 -Dorg.eclipse.jetty.util.URI.charset=GB ...

  5. 使用javascript实现的一些功能

    原文:使用javascript实现的一些功能 今天学习了javascript中的事件,已经接近尾声,可以说明天跨入jquery的学习啦,学习了一周的javascript,感觉还没有掌握其中学习的微妙之 ...

  6. 6、Cocos2dx 3.0游戏开发的基本概念找个小三场比赛

    重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27689713 郝萌主友情提示: 人是习惯的产物,当你 ...

  7. asp.net webForm 前后台类关系

    要研究这个,首先要新建网站, protected void Page_Load(object sender, EventArgs e) { string str = System.Reflection ...

  8. C语言编写Windows服务程序

    原文:C语言编写Windows服务程序 #include <Windows.h> #include <stdio.h> #define SLEEP_TIME 5000 // 间 ...

  9. 移动小bug

    1. 在三星note2,小米2,页面加载后,页面有黑块. 那么提高被盖住的部分z-index. 2. iphone5 ,ios7.0.4,上文字显示不出 那么就先hide,setTimeout几百毫秒 ...

  10. Erlang运行时的错误

    Erlang运行时发生错误时,会返回一些错误信息,理解这些信息,对于学好.用好Erlang来说是必要. Erlang中的运行错误包括:badarg, badarith, badmatch, funct ...