我想以编程方式布局一个应适合屏幕宽度的 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)
结果如下:
标签是按预期使用系统间距作为左插图布局的,但它的 trailingAnchor
似乎等于 superview 的 trailingAnchor
而不是添加系统两者之间的间距。
我已经尝试使用 constraintEqualToSystemSpacingAfter
和 constraintGreaterThanOrEqualToSystemSpacingAfter
但得到了相同的结果。
关于如何获取标签与其父 View 的尾随 anchor 之间的系统间距有什么想法吗?
最佳答案
像这样颠倒顺序
view.safeAreaLayoutGuide.trailingAnchor.constraintEqualToSystemSpacingAfter(statusLabel.trailingAnchor, multiplier: 1).isActive = true
首先查看状态标签,然后查看。
关于ios 将 constraintLessThanOrEqualToSystemSpacingAfter 用于 trailingAnchor 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49730007/