One label's constraints added using IB, one label's constraints added programmatically

我们两清 提交于 2019-12-25 07:34:52

问题


My goal is to position two labels on top of each other. I dragged a label onto the storyboard, labelA, and I pinned it a random distance from the top, and I centered labelA horizontally in the view, resulting in blue constraints.

Then I dragged a second label, labelB, onto the storyboard and moved it below labelA (say, 1 inch separating the two labels). Now, I am trying to programmatically set the constraints for labelB to move it on top of 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
    }

}

However, when I run my app iOS breaks some of the constraints, and what I see is two labels equidistant from the top, but next to each other horizontally. In addition, labelB pulls labelA down to its y-position. What I want to see is labelA stay where I initially constrained it, with labelB on top of labelA.

Is it possible to set one subview's constraints using IB and set another subview's constraints using code?

output:

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:


Well, I read a question about removing all constraints from a view, and adding the following code succeeds in doing what I want:

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
}

I also tried the code above and commented out the line:

labelB.translatesAutoresizingMaskIntoConstraints = false

and it works the same way.

Can anyone explain what constraints I am removing? Thanks.



来源:https://stackoverflow.com/questions/37912558/one-labels-constraints-added-using-ib-one-labels-constraints-added-programmat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!