十一岁的加重songxing10000…… ------ 回到博主首页

纯手码自动布局

#import "ViewController.h"
#define TextFieldFrame CGRectMake(0, 100, 100, 30)

@interface ViewController ()

@property (nonatomic, strong) UIButton    *leftButton;
@property (nonatomic, strong) UIButton    *rightButton;
@property (nonatomic, strong) UITextField *textfield;

@end

@implementation ViewController

#pragma mark - life cycle
- (void)viewDidLoad{
    [super viewDidLoad];
    
    UIView *superview = self.view;
    
    [superview addSubview:self.leftButton];
    [self addLeftButtonConstraintsInView:superview];
    
    [superview addSubview:self.rightButton];
    [self addRightButtonConstraintsInView:superview];
    
    [superview addSubview:self.textfield];
    [self addTextfieldConstraintsInView:superview];

    
}

#pragma mark - private methods
- (void)addLeftButtonConstraintsInView:(UIView *)superview
{
    NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint constraintWithItem:self.leftButton
                                                                             attribute:NSLayoutAttributeCenterX
                                                                             relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                                                toItem:superview
                                                                             attribute:NSLayoutAttributeCenterX
                                                                            multiplier:1.0
                                                                              constant:-60.0f];
    NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint constraintWithItem:self.leftButton
                                                                             attribute:NSLayoutAttributeCenterY
                                                                             relatedBy:NSLayoutRelationEqual
                                                                                toItem:superview
                                                                             attribute:NSLayoutAttributeCenterY
                                                                            multiplier:1.0f
                                                                              constant:0.0f];
    [superview addConstraints:@[ leftButtonXConstraint,
                                 leftButtonYConstraint]];

}

- (void)addRightButtonConstraintsInView:(UIView *)superview
{
    NSLayoutConstraint *rightButtonXConstraint = [NSLayoutConstraint constraintWithItem:self.rightButton
                                                                              attribute:NSLayoutAttributeCenterX
                                                                              relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                                                 toItem:superview
                                                                              attribute:NSLayoutAttributeCenterX
                                                                             multiplier:1.0
                                                                               constant:60.0f];
    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]];
}
- (void)addTextfieldConstraintsInView:(UIView *)superview
{
    
    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]];

}

#pragma mark - getters and setters
-(UIButton *)leftButton
{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _leftButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_leftButton setTitle:@"LeftButton" forState:UIControlStateNormal];
    }
    return _leftButton;
}

- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _rightButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_rightButton setTitle:@"RightButton" forState:UIControlStateNormal];
    }
    return _rightButton;
}

- (UITextField *)textfield
{
    if (!_textfield) {
        _textfield = [[UITextField alloc]initWithFrame:TextFieldFrame];
        _textfield.borderStyle = UITextBorderStyleRoundedRect;
        _textfield.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _textfield;
}
@end

 

posted @ 2015-05-17 10:32  songxing10000  阅读(227)  评论(0编辑  收藏  举报