ios 将 constraintLessThanOrEqualToSystemSpacingAfter 用于 trailingAnchor 的正确方法

标签 ios autolayout uilabel

我想以编程方式布局一个应适合屏幕宽度的 UILabel,系统间距为左右插图。这是我的代码:

statusLabel.font = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)
statusLabel.numberOfLines = 0
statusLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(statusLabel)

statusLabel.topAnchor.constraint(equalTo: otherView.bottomAnchor, constant: 0),
statusLabel.leadingAnchor.constraintEqualToSystemSpacingAfter(view.safeAreaLayoutGuide.leadingAnchor, multiplier: 1),
statusLabel.trailingAnchor.constraintLessThanOrEqualToSystemSpacingAfter(view.safeAreaLayoutGuide.trailingAnchor, multiplier: 1),
statusLabel.bottomAnchor.constraint(equalTo: someOtherView.topAnchor, constant: 0)

结果如下:

enter image description here

标签是按预期使用系统间距作为左插图布局的,但它的 trailingAnchor 似乎等于 superview 的 trailingAnchor 而不是添加系统两者之间的间距。

我已经尝试使用 constraintEqualToSystemSpacingAfterconstraintGreaterThanOrEqualToSystemSpacingAfter 但得到了相同的结果。

关于如何获取标签与其父 View 的尾随 anchor 之间的系统间距有什么想法吗?

最佳答案

像这样颠倒顺序

view.safeAreaLayoutGuide.trailingAnchor.constraintEqualToSystemSpacingAfter(statusLabel.trailingAnchor, multiplier: 1).isActive = true

首先查看状态标签,然后查看。

关于ios 将 constraintLessThanOrEqualToSystemSpacingAfter 用于 trailingAnchor 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49730007/

相关文章:

ios - 自动布局约束以改变方向的组成

ios - iOS坐标系在设备之间是否相等?

ios - 为什么 UILabel() 返回 nil?

iOS比较标签的值(value)

iphone - 在 xcode 中将动态大小设置为 UILabels?

ios - 模态转场会创建新对象吗?

ios - 未调用 MFMailComposeViewController

ios - 为什么 SCNPhysicsContact.collisionImpulse 总是 0.0?

ios - 具有比例宽度和文本驱动高度的动态 UILabel

ios - UITableViewCell 被重用并在 UIView 中显示错误的绘图