iphone - 为布局约束添加优先级

标签 iphone ios objective-c autolayout

我在这样的 superView 中有一个 labelbutton

|--------------[Label]-----[button]-|

如果可能的话,我希望标签居中,然后与按钮保持最小距离并向左移动。

所以如果按钮很大,它看起来像...

|-[        LABEL!        ]-[button]-|

因此,按钮将保持原样且大小不变。元素之间的间隙最小。

我可以添加 centerX 约束,但我似乎无法赋予它优先级,因此它仍然是 Required

我怎样才能创造这种情况?我正在代码中进行所有自动布局。

我目前的限制是...

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_label]-(>=8@1000)-[_button(==45)]-|"
                                                             options:NSLayoutFormatAlignAllCenterY
                                                             metrics:nil
                                                               views:views]];

[self addConstraint:[NSLayoutConstraint constraintWithItem:_label
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self.contentView
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0
                                                  constant:0.0]];

但我不确定如何降低第二个约束的优先级。

最佳答案

您只需设置约束的 priority 属性,如下所示:

NSLayoutConstraint *centeringConstraint = 
    [NSLayoutConstraint constraintWithItem:_label
                                 attribute:NSLayoutAttributeCenterX
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.contentView
                                 attribute:NSLayoutAttributeCenterX
                                multiplier:1.0
                                  constant:0.0];

centeringConstraint.priority = 800; // <-- this line

[self addConstraint:centeringConstraint];

关于iphone - 为布局约束添加优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15365286/

相关文章:

iphone - 我如何以编程方式使单击 UIImageView 在 iPhone 上执行某些操作?

ios - IOS 中的 XMPP 框架

iphone - 如何让 asl_log 出现在 iOS 系统控制台输出中?

iOS 9 Facebook 访问 token 在未来的应用程序发布中为零

objective-c - 获取当前正在播放的轨道名称

iphone - UIWebView 和编码的 HTML 字符串?

iphone - 如何将 FBGraphUser 对象传递给 appDelegate

android - 使用 Cordova FileTransfer 将文件保存到公共(public)目录

iphone - 转换后的 MP3 格式不支持 11025 和 22050 采样率

ios - 在 iOS 7 上,如果最终包含在选项卡栏 Controller 中,则使用工具栏推送 Controller 会留下不可用空间的间隙