• 纯手码自动布局


    #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
  • 相关阅读:
    python 爬取豆瓣电影短评并wordcloud生成词云图
    基于javaweb人脸识别注册登录系统
    html/jsp导出pdf格式的几种方法(jsPDF,iText,wkhtmltopdf)
    微信小程序wx.getLocation()获取经纬度及JavaScript SDK调用腾讯地图API获取某一类地址
    微信小程序登录流程及解析用户openid session_key,获取用户信息
    Windows Server 2012 R2服务器部署Tomcat JDK、安装Mysql以及将Java项目部署到CVM
    Tomcat出现端口占用错误
    linux cmake error undefined reference to symbol 'pthread_create@@GLIBC_2.2.5
    CMake Warning This command specifies the relative path as a link directory.
    linux vim查看文件编码格式
  • 原文地址:https://www.cnblogs.com/songxing10000/p/4509239.html
Copyright © 2020-2023  润新知