gpt4 book ai didi

ios - 使用 IN 添加一个标签的约束,以编程方式添加一个标签的约束

转载 作者:行者123 更新时间:2023-11-30 13:19:20 25 4
gpt4 key购买 nike

我的目标是将两个标签放在一起。我将一个标签 labelA 拖到 Storyboard 上,并将其固定在距顶部随机距离的位置,并将 labelA 在 View 中水平居中,从而产生蓝色约束。

然后,我将第二个标签 labelB 拖到 Storyboard 上,并将其移动到 labelA 下方(例如,将两个标签分开 1 英寸)。现在,我尝试以编程方式设置 labelB 的约束,以将其移动到 labelA 之上:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var labelA: UILabel!
@IBOutlet weak var labelB: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

labelB.translatesAutoresizingMaskIntoConstraints = false
//labelA.translatesAutoresizingMaskIntoConstraints = false

labelB.topAnchor.constraintEqualToAnchor(
labelA.topAnchor
).active = true

labelB.centerXAnchor.constraintEqualToAnchor(
view.centerXAnchor
).active = true
}

}

但是,当我运行我的应用程序时,iOS 打破了一些限制,我看到的是两个标签与顶部等距,但水平相邻。此外,labelB 将 labelA 向下拉至其 y 位置。我想看到的是 labelA 停留在我最初限制它的位置,而 labelB 位于 labelA 之上。

是否可以使用 IB 设置一个 subview 的约束并使用代码设置另一个 subview 的约束?

输出:

2016-06-19 16:42:42.804 Test4[40792:232886] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<_UILayoutSupportConstraint:0x7fe8d2c13b20 V:[_UILayoutGuide:0x7fe8d2c1caa0(0)]>",
"<_UILayoutSupportConstraint:0x7fe8d2c245a0 V:|-(0)-[_UILayoutGuide:0x7fe8d2c1caa0] (Names: '|':UIView:0x7fe8d2c15790 )>",
"<NSLayoutConstraint:0x7fe8d2c253e0 V:[_UILayoutGuide:0x7fe8d2c1caa0]-(111)-[UILabel:0x7fe8d2f169a0'LabelA']>",
"<NSLayoutConstraint:0x7fe8d2c26e70 UILabel:0x7fe8d2c22a50'LabelB'.top == UILabel:0x7fe8d2f169a0'LabelA'.top>",
"<NSIBPrototypingLayoutConstraint:0x7fe8d2c25a20 'IB auto generated at build time for view with fixed frame' V:|-(256)-[UILabel:0x7fe8d2c22a50'LabelB'] (Names: '|':UIView:0x7fe8d2c15790 )>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fe8d2c253e0 V:[_UILayoutGuide:0x7fe8d2c1caa0]-(111)-[UILabel:0x7fe8d2f169a0'LabelA']>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-06-19 16:42:42.807 Test4[40792:232886] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x7fe8d2e24ba0 UILabel:0x7fe8d2c22a50'LabelB'.centerX == UIView:0x7fe8d2c15790.centerX>",
"<NSIBPrototypingLayoutConstraint:0x7fe8d2c254d0 'IB auto generated at build time for view with fixed frame' H:|-(288)-[UILabel:0x7fe8d2c22a50'LabelB'](LTR) (Names: '|':UIView:0x7fe8d2c15790 )>",
"<NSIBPrototypingLayoutConstraint:0x7fe8d2c25a70 'IB auto generated at build time for view with fixed frame' H:[UILabel:0x7fe8d2c22a50'LabelB'(52.5)]>",
"<NSLayoutConstraint:0x7fe8d2d8b020 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fe8d2c15790(375)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fe8d2e24ba0 UILabel:0x7fe8d2c22a50'LabelB'.centerX == UIView:0x7fe8d2c15790.centerX>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

最佳答案

嗯,我读了一个关于 removing all constraints from a view 的问题,并添加以下代码成功完成我想要的操作:

override func viewDidLoad() {
super.viewDidLoad()

labelB.removeFromSuperview() //<*****HERE
view.addSubview(labelB) //<*****HERE

labelB.translatesAutoresizingMaskIntoConstraints = false
//labelA.translatesAutoresizingMaskIntoConstraints = false

labelB.topAnchor.constraintEqualToAnchor(
labelA.topAnchor
).active = true

labelB.centerXAnchor.constraintEqualToAnchor(
view.centerXAnchor
).active = true
}

我还尝试了上面的代码并注释掉了该行:

labelB.translatesAutoresizingMaskIntoConstraints = false

其工作方式相同。

谁能解释一下我要消除哪些限制?谢谢。

关于ios - 使用 IN 添加一个标签的约束,以编程方式添加一个标签的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37912558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com