使用IB添加了一个标签的约束,以编程方式添加了一个标签的约束

时间:2016-06-19 22:40:47

标签: ios swift autolayout ios9

我的目标是将两个标签放在彼此的顶部。我将一个标签拖到故事板上,标签为A,然后我将它固定在距离顶部的一个随机距离处,我将labelA水平居中放在视图中,导致蓝色约束。

然后我将第二个标签labelB拖到故事板上并将其移动到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设置一个子视图的约束,并使用代码设置另一个子视图的约束?

输出:

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.

1 个答案:

答案 0 :(得分:0)

好吧,我读了一个关于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

它的工作方式相同。

任何人都可以解释我要删除的约束吗?感谢。

相关问题