【问题标题】:Autolayout - UIView does not resize correctly to subview's size自动布局 - UIView 无法正确调整为子视图的大小
【发布时间】:2023-03-23 22:04:01
【问题描述】:

我正在尝试使用自动布局以编程方式创建 UIView 和 UILabel 作为子视图。

约束

我在view 上使用了以下约束:

  • CenterX of view 到 fastAttacksContainerView (superview)。

  • Top 常量 8 到 superview 的约束。

  • 然后创建了一个label,它是view的子视图,并为Top、Bottom、Left,以及权利到view。

问题

view 仅调整到标签的 frame 大小,并没有考虑所有 4 个边上的常量 8 的 4 个约束。这会导致label 部分显示在view 之外。

let view = UIView()
view.backgroundColor = pokemon.secondaryColor

let label = UILabel()

fastAttacksContainerView.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false

label.text = fa

let gest = UITapGestureRecognizer(target: self, action: #selector(self.selectFastAttack))
view.addGestureRecognizer(gest)

fastAttackButtons.append(view)
fastAttackLables.append(label)

let top = NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal, toItem: fastAttacksContainerView, attribute: .Top, multiplier: 1, constant: 8)
let centerX = NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: fastAttacksContainerView, attribute: .CenterX, multiplier: 1, constant: 0)

let labLeft = NSLayoutConstraint(item: label, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1, constant: 8)
let labTop = NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 8)
let labRigth = NSLayoutConstraint(item: label, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1, constant: 8)
let labBottom = NSLayoutConstraint(item: label, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1, constant: 8)
view.addConstraints([labLeft, labTop, labRigth, labBottom])
fastAttacksContainerView.addConstraints([top, centerX])

输出

【问题讨论】:

  • 为什么不使用情节提要?
  • 上述代码是在运行时获取views的个数进行迭代的。

标签: ios swift uiview autolayout uilabel


【解决方案1】:

view 的高度不明确。

您应该为view 的高度或从它到fastAttacksContainerView 底部的距离添加约束。

【讨论】:

  • view 的高度不等于label 顶部 + 8 和底部 + 8 的高度吗?
  • 尝试用label.addConstraints([labLeft, labTop, labRigth, labBottom])替换view.addConstraints([labLeft, labTop, labRigth, labBottom])
  • 程序因The view hierarchy is not prepared for the constraint错误而崩溃。
  • 把fastAttacksContainerView.addConstraints...放在view.addConstraints...之前
  • 我添加了一个对我有用的答案。似乎有一个错误,我将item 和toItem 交换为Leading 和Trailing,并且我按预期工作。
【解决方案2】:

我设法通过在情节提要上进行类似设置并复制约束来解决此问题。

我改变的地方:

  • 使用Leading 和Trailing 代替Left 和Right 布局属性。
  • 对于labLeft 和labRight 约束,我交换了item 和toItem。 (这对我来说似乎是一个错误。有人可以验证吗?)

代码更改:

let labLeft = NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1, constant: 8)
let labTop = NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 8)
let labRigth = NSLayoutConstraint(item: view, attribute: .Trailing, relatedBy: .Equal, toItem: label, attribute: .Trailing, multiplier: 1, constant: 8)
let labBottom = NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1, constant: 8)

【讨论】:

    猜你喜欢
    • 2013-01-22
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多