码迷,mamicode.com
首页 > 其他好文 > 详细

AutoLayout

时间:2015-06-01 16:48:00      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

//
//  ViewController.m
//  AutoLayoutByCode
//
//  Created by sunlihuo on 15/6/1.
//  Copyright (c) 2015年 sunlihuo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) UIView *view1;
@property (strong, nonatomic) UIView *view2;
@property (strong, nonatomic) UIView *view3;
@property (strong, nonatomic) UISlider *slider;

@property (strong, nonatomic) NSLayoutConstraint *view1WidthC;
@property (strong, nonatomic) NSLayoutConstraint *view1HeightC;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self onCreate];
    [self createConstraint];
    
}

#pragma mark 创建界面控件
- (void)onCreate {
    //创建view1
    self.view1 = [[UIView alloc]initWithFrame:CGRectZero];
    self.view1.backgroundColor = [UIColor redColor];
    //创建view2
    self.view2 = [[UIView alloc]initWithFrame:CGRectZero];
    self.view2.backgroundColor = [UIColor redColor];
    //创建slider
    self.slider = [[UISlider alloc]initWithFrame:CGRectZero];
    self.slider.maximumValue = 120;
    self.slider.minimumValue = 80;
    self.slider.value = 80;
    [self.slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    //创建view3
    self.view3 = [[UIView alloc]initWithFrame:CGRectZero];
    self.view3.backgroundColor = [UIColor redColor];
    //加入view
    [self.view addSubview:self.view1];
    [self.view addSubview:self.view2];
    [self.view addSubview:self.slider];
    [self.view addSubview:self.view3];
    
}

#pragma mark 创建约束
- (void)createConstraint {
    //设置需要添加约束的view的autoresizing属性为NO
    self.view1.translatesAutoresizingMaskIntoConstraints = NO;
    self.view2.translatesAutoresizingMaskIntoConstraints = NO;
    self.slider.translatesAutoresizingMaskIntoConstraints = NO;
    self.view3.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSDictionary *dictC = @{@"view1":self.view1, @"view2":self.view2, @"slider":self.slider, @"view3":self.view3};
    
    //view1的约束
    NSArray *view1TopC = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view1]" options:kNilOptions metrics:nil views:dictC];
    
    NSArray *view1WidthC = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view1(80.0)]" options:0 metrics:nil views:dictC];
    
    NSArray *view1HightC = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view1(80.0)]" options:0 metrics:nil views:dictC];
    self.view1WidthC = view1WidthC[0];
    self.view1HeightC = view1HightC[0];
    
    [self.view addConstraints:view1TopC];
    [self.view addConstraints:view1WidthC];
    [self.view addConstraints:view1HightC];
    
    //view2的约束
    NSArray *view2TopC = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view1]-20-[view2]" options:kNilOptions metrics:nil views:dictC];
    NSArray *view2LeftRightC = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-60-[view2]-60-|" options:0 metrics:nil views:dictC];
    NSArray *view2HeightC = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view2(44)]" options:0 metrics:nil views:dictC];
    NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view2 attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraints:view2TopC];
    [self.view addConstraints:view2LeftRightC];
    [self.view addConstraints:view2HeightC];
    [self.view addConstraint:centerX];
    
    //添加slider的约束
    NSArray *sliderTopC = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view2]-20-[slider]" options:0 metrics:nil views:dictC];
    NSArray *sliderLeftRightC = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-60-[slider]-60-|" options:0 metrics:nil views:dictC];
    NSArray *sliderHeightC = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[slider(30)]" options:0 metrics:nil views:dictC];
    [self.view addConstraints:sliderTopC];
    [self.view addConstraints:sliderLeftRightC];
    [self.view addConstraints:sliderHeightC];
    
    //添加view3的约束
    NSArray *view3TopC = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[slider]-20-[view3]" options:0 metrics:nil views:dictC];
    NSArray *view3LeftRightC = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-60-[view3]-60-|" options:0 metrics:nil views:dictC];
    NSLayoutConstraint *view3HeightC = [NSLayoutConstraint constraintWithItem:self.view3 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.view2 attribute:NSLayoutAttributeHeight multiplier:2 constant:0];
    [self.view addConstraints:view3TopC];
    [self.view addConstraints:view3LeftRightC];
    [self.view addConstraint:view3HeightC];
    
}
#pragma mark 根据滑块滑到改变第一个视图的宽高约束
- (void)sliderValueChanged:(UISlider *)sender{
    //1.清除第一个视图原有的宽高约束
    [self.view removeConstraint:self.view1WidthC];
    [self.view removeConstraint:self.view1HeightC];
    
    NSDictionary *dictC = @{@"view1":self.view1, @"view2":self.view2, @"slider":self.slider, @"view3":self.view3};
    
    NSArray *view1WidthC = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[view1(%f)]", sender.value] options:0 metrics:nil views:dictC];
    
    NSArray *view1HightC = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[view1(%f)]", sender.value] options:0 metrics:nil views:dictC];
    
    self.view1WidthC = view1WidthC[0];
    self.view1HeightC = view1HightC[0];
    
    [self.view addConstraints:view1WidthC];
    [self.view addConstraints:view1HightC];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

AutoLayout

标签:

原文地址:http://blog.csdn.net/sunlihuo/article/details/46311241

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!