IOSレイアウトノート3(異なる親のviewで拘束)

6737 ワード

前の記事:IOSレイアウトノート2(Visual Format Language定義水平および垂直拘束)
最終効果図は次のとおりです.
多くの制限条件がビューに適用されています.説明します.
・我々のビューコントローラのメインビューには2つのグレーのビューがあります.2つのビューピッチビューコントローラのビュー左
エッジと右側には標準的な空白距離があります.ビューの上部は、上部のビューの上部から標準的な空白の距離を持つ必要があります.2つのグレービューの間には、標準的な垂直空白距離が必要です.・2つのグレービューの垂直中央にボタンが1つ必要です.・上の灰色のビューのボタンは、親ビューの左側に標準的な空白の距離があります.・下のグレービューのボタンの左の境界は、上のグレービューのボタンの左の境界と一致する必要があります.これがクロスビューの制約です.これは私たちにとって重要です.・灰色のビューは、ビューコントローラの方向の変化に応じて、新たにサイズを変更する必要があります.・2つのグレーのビューの高さは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レイアウトノート4(IOS 7自動レイアウトの制約設定)