• 纯手码自动布局


    #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
  • 相关阅读:
    Ubuntu下手动安装vscode
    VMware Tools安装后设置自动挂载解决共享文件夹无法显示的问题
    VMware Tools安装方法及共享文件夹设置方法
    JavaScript原始类型转换和进制转换
    Javascript的数据类型(原始类型和引用类型)
    设计模式(六)观察者模式
    设计模式(五)之适配器模式
    设计模式(四)注册模式 解决:解决全局共享和交换对象
    设计模式(三)单例模式
    设计模式(二)之策略模式
  • 原文地址:https://www.cnblogs.com/songxing10000/p/4509239.html
Copyright © 2020-2023  润新知