【问题标题】:ios correct way to use constraintLessThanOrEqualToSystemSpacingAfter for trailingAnchorios 使用 constraintLessThanOrEqualToSystemSpacingAfter 进行 trailingAnchor 的正确方法
【发布时间】:2018-04-09 09:46:54
【问题描述】:

我想以编程方式布局一个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 似乎等于超级视图的trailingAnchor,而不是在两者之间添加系统间距。

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

关于如何获得标签与其超级视图的尾随锚点之间的系统间距的任何想法?

【问题讨论】:

    标签: ios autolayout uilabel


    【解决方案1】:

    像这样颠倒顺序

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

    先查看,然后查看 statusLabel。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 2019-03-19
      • 2021-11-12
      • 2014-07-02
      相关资源
      最近更新 更多