IOS布局笔记三(使用不同父类的 view 进行约束)
2014-05-04 09:52
495 查看
上一篇文章:IOS布局笔记二( Visual Format Language 定义水平和垂直约束)
最终效果图如下:
很多限制条件都已经应用到了视图中,我们在解释一下:
·在我们的视图控制器的主视图中有两个灰色的视图。两个视图距视图控制器的视图左
边和右边都有一个标准的空白距离。视图的顶部距顶部的视图的顶部必须有一个标准的空白
距离。在两个灰色视图之间要有一个标准的垂直空白距离。
·在两个灰色视图里的垂直中央都要有一个按钮。
·在上面的灰色视图中的按钮距其父视图的左边要有一个标准的空白距离。
·在下面的灰色视图中的按钮的左边界应该和上面的灰色视图中的按钮的左边界对齐。
这就是交叉视图约束条件,这对我们来说是很重要的。
·灰色视图应该根据视图控制器方向的改变而进行从新调整大小。 ·两个灰色视图的高度必须是 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布局笔记四(IOS7配置自动布局的约束)
最终效果图如下:
很多限制条件都已经应用到了视图中,我们在解释一下:
·在我们的视图控制器的主视图中有两个灰色的视图。两个视图距视图控制器的视图左
边和右边都有一个标准的空白距离。视图的顶部距顶部的视图的顶部必须有一个标准的空白
距离。在两个灰色视图之间要有一个标准的垂直空白距离。
·在两个灰色视图里的垂直中央都要有一个按钮。
·在上面的灰色视图中的按钮距其父视图的左边要有一个标准的空白距离。
·在下面的灰色视图中的按钮的左边界应该和上面的灰色视图中的按钮的左边界对齐。
这就是交叉视图约束条件,这对我们来说是很重要的。
·灰色视图应该根据视图控制器方向的改变而进行从新调整大小。 ·两个灰色视图的高度必须是 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布局笔记四(IOS7配置自动布局的约束)
相关文章推荐
- IOS使用不同父类的 view 进行约束
- 使用不同父类的 view 进行约束
- iOS 学习笔记 五 (2015.03.17)使用storyBoard进行tableview的跳转
- 如何在IOS中使用实现Android view.gone 功能(swift版)cell(两种不同的布局)
- 使用ListView进行不同布局ItemView的显示
- iOS YYText的使用笔记一(YYTextView图文编辑器)
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- android Listview的getView和convertview原理解析以及使用不同的item布局(二)
- WebViewJavascriptBridge使用(ios自学笔记)
- iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
- 如何使用AutoLayout对两个以及两个以上的平行或者相互依赖的view进行布局
- ios学习笔记:UIAlertView的使用
- ios自动布局使用代码添加约束
- iOS回顾笔记(03) -- 自定义View的封装和xib文件的使用详解
- Swift - 使用网格(UICollectionView)进行流布局
- IOS应用加载本地WebView禁止进行缓存(使用私有api)
- iOS中使用的tableview为group形式时如何设置不同sections的间距
- iOS 自动布局 Auto Layout 入门 03 进一步学习使用约束
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- iOS中Xcode使用UIScrollView+AutoLayout轻松实现滚动布局