IOS使用不同父类的 view 进行约束
2014-05-04 09:52 Lves Li 阅读(301) 评论(0) 编辑 收藏 举报
最终效果图如下:
很多限制条件都已经应用到了视图中,我们在解释一下:
·在我们的视图控制器的主视图中有两个灰色的视图。两个视图距视图控制器的视图左
·在我们的视图控制器的主视图中有两个灰色的视图。两个视图距视图控制器的视图左
边和右边都有一个标准的空白距离。视图的顶部距顶部的视图的顶部必须有一个标准的空白
距离。在两个灰色视图之间要有一个标准的垂直空白距离。
·在两个灰色视图里的垂直中央都要有一个按钮。
·在上面的灰色视图中的按钮距其父视图的左边要有一个标准的空白距离。
·在下面的灰色视图中的按钮的左边界应该和上面的灰色视图中的按钮的左边界对齐。
这就是交叉视图约束条件,这对我们来说是很重要的。
·灰色视图应该根据视图控制器方向的改变而进行从新调整大小。 ·两个灰色视图的高度必须是 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