iOS自动布局

#import

@interface ViewController : UIViewController

@property (nonatomic, strong) UIButton *leftButton;

@property (nonatomic, strong) UIButton *rightButton;

@property (nonatomic, strong) UITextField *textfield;

@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIView *superview = self.view;

/*1. Create leftButton and add to our view*/

//创建左边的按钮并且添加到视图上

self.leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//自动调整按钮的约束

self.leftButton.translatesAutoresizingMaskIntoConstraints = NO;

[self.leftButton setTitle:@"LeftButton" forState:UIControlStateNormal];

[self.view addSubview:self.leftButton];

/* 2. Constraint to position LeftButton's X*/

//控制左边按钮的 x位置  LayoutConstraint 布局约束

NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute: NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f];

/* 3. Constraint to position LeftButton's Y*/

//控制左边按钮的 y位置  LayoutConstraint 布局约束

NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterY

relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];

/* 4. Add the constraints to button's superview*/

//添加按钮的父视图的限制

[superview addConstraints:@[leftButtonXConstraint,leftButtonYConstraint]];

/*5. Create rightButton and add to our view*/

//创建右边的按钮并且添加到视图上

self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;

[self.rightButton setTitle:@"RightButton" forState:UIControlStateNormal];

[self.view addSubview:self.rightButton];

/*6. Constraint to position RightButton's X*/

//控制右边按钮的 x位置  LayoutConstraint 布局约束

NSLayoutConstraint *rightButtonXConstraint =[NSLayoutConstraint constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterX

relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute: NSLayoutAttributeCenterX multiplier:1.0 constant:60.0f];

/*7. Constraint to position RightButton's Y*/

//控制右边按钮的 y位置  LayoutConstraint 布局约束

rightButtonXConstraint.priority =UILayoutPriorityDefaultHigh;

NSLayoutConstraint *centerYMyConstraint = [NSLayoutConstraint constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterY

relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];

[superview addConstraints:@[centerYMyConstraint,rightButtonXConstraint]];

//8. Add Text field

//添加文本框

self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, 100, 30)];

self.textfield.borderStyle =1;

self.textfield.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:self.textfield];

//9. Text field Constraints

//文本字段的约束 顶部

NSLayoutConstraint *textFieldTopConstraint = [NSLayoutConstraint constraintWithItem:self.textfield attribute:NSLayoutAttributeTop

relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:60.0f];

//文本字段的约束 底部

NSLayoutConstraint *textFieldBottomConstraint = [NSLayoutConstraint

constraintWithItem:self.textfield attribute:NSLayoutAttributeTop

relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.rightButton

attribute:NSLayoutAttributeTop multiplier:0.8 constant:-60.0f];

//文本字段的约束 左部

NSLayoutConstraint *textFieldLeftConstraint = [NSLayoutConstraint

constraintWithItem:self.textfield attribute:NSLayoutAttributeLeft

relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:30.0f];

//文本字段的约束 右部

NSLayoutConstraint *textFieldRightConstraint = [NSLayoutConstraint

constraintWithItem:self.textfield attribute:NSLayoutAttributeRight

relatedBy:NSLayoutRelationEqual toItem:superview attribute:

NSLayoutAttributeRight multiplier:1.0 constant:-30.0f];

[superview addConstraints:@[textFieldBottomConstraint ,textFieldLeftConstraint, textFieldRightConstraint,textFieldTopConstraint]];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

你可能感兴趣的:(iOS自动布局)