iOS Autolayout 约束设置【顺序】的重要性!

0x00 顺序不同,结果不同

看图说话 1

代码是这样滴~

设置好约束,让 4 个按钮,宽度均分~

结果如上图

    [_pastButton.topAnchor constraintEqualToAnchor:_textView.bottomAnchor constant:6].active = YES;
    [_pastButton.leftAnchor constraintEqualToAnchor:_textView.leftAnchor].active = YES;
    [_pastButton.heightAnchor constraintEqualToConstant:44].active = YES;
    
    NSArray *buttons = @[_pastButton, _deleteButton, _breakButton, _inputButton];
    for (NSInteger i = 0; i < buttons.count-1; i++) {
        UIButton *button1 = buttons[i];
        UIButton *button2 = buttons[i+1];
        
        [button2.topAnchor constraintEqualToAnchor:button1.topAnchor].active = YES;
        [button2.leftAnchor constraintEqualToAnchor:button1.rightAnchor constant:10].active = YES;
        [button2.heightAnchor constraintEqualToAnchor:button1.heightAnchor].active = YES;
        [button2.widthAnchor constraintEqualToAnchor:button1.widthAnchor].active = YES;
        
        if (i == buttons.count-1) {
            [button2.rightAnchor constraintEqualToAnchor:_textView.rightAnchor].active = YES;
        }
    }

看图说话 2

代码是这样滴~

设置好约束,让 4 个按钮,宽度均分~

结果如上图,完美!

    [_pastButton.topAnchor constraintEqualToAnchor:_textView.bottomAnchor constant:6].active = YES;
    [_pastButton.leftAnchor constraintEqualToAnchor:_textView.leftAnchor].active = YES;
    [_pastButton.heightAnchor constraintEqualToConstant:44].active = YES;
    
    [_inputButton.rightAnchor constraintEqualToAnchor:_textView.rightAnchor].active = YES;
    
    NSArray *buttons = @[_pastButton, _deleteButton, _breakButton, _inputButton];
    for (NSInteger i = 0; i < buttons.count-1; i++) {
        UIButton *button1 = buttons[i];
        UIButton *button2 = buttons[i+1];
        
        [button2.topAnchor constraintEqualToAnchor:button1.topAnchor].active = YES;
        [button2.leftAnchor constraintEqualToAnchor:button1.rightAnchor constant:10].active = YES;
        [button2.heightAnchor constraintEqualToAnchor:button1.heightAnchor].active = YES;
        [button2.widthAnchor constraintEqualToAnchor:button1.widthAnchor].active = YES;
    }

0x02 代码区别

图 1 的代码,是在最后一步添加,最后一个按钮,的右侧约束

        if (i == buttons.count-1) {
            [button2.rightAnchor constraintEqualToAnchor:_textView.rightAnchor].active = YES;
        }

图 2 的代码,是在开始添加,最后一个按钮,的右侧约束

        [_inputButton.rightAnchor constraintEqualToAnchor:_textView.rightAnchor].active = YES;

可见,

约束设置的顺序,是多么滴重要!


0x03 我的小作品

欢迎体验我的作品之一:小五笔 86 版

五笔学习好帮手
App Store 搜索即可~


相关推荐
m0_748238922 小时前
webgis入门实战案例——智慧校园
开发语言·ios·swift
tanghongchang1235 小时前
Mac Android studio 升级LadyBug 版本,所产生的bug
macos·android studio·ladybug
Legendary_0086 小时前
LDR6020在iPad一体式键盘的创新应用
ios·计算机外设·ipad
iBaoxing13 小时前
mac 使用 launchctl 实现每次登录系统时 frpc 就会自动启动
macos
鹏阿鹏14 小时前
【Mac】终端改色-让用户名和主机名有颜色
macos
/**书香门第*/15 小时前
Laya ios接入goole广告,搭建环境 1
ios
易我数据恢复大师18 小时前
怎么设置电脑密码?Windows和Mac设置密码的方法
windows·macos·电脑
wakangda21 小时前
React Native 集成 iOS 原生功能
react native·ios·cocoa
几维安全1 天前
如何保护你的 iOS 应用免受逆向工程攻击
macos·objective-c·cocoa
18号房客1 天前
macOS开发环境配置与应用开发教程(一)
vscode·macos·visualstudio·eclipse·intellij-idea·phpstorm·visual studio