zoukankan      html  css  js  c++  java
  • 纯手码自动布局

    #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
  • 相关阅读:
    Jetty和tomcat的比较
    Spring Boot – Jetty配置
    Java规则之条件语句中做空判断时使用||和&&常犯的错误
    bboss oreach循环嵌套遍历map
    url全部信息打印
    ajax省市县三级联动
    关于mysql中的count()函数
    vue——统一配置axios的baseUrl和所有请求的路径
    js——substr与substring的区别
    vue——axios请求成功却进入catch的原因
  • 原文地址:https://www.cnblogs.com/songxing10000/p/4509239.html
Copyright © 2011-2022 走看看