使用IB添加一个标签的约束,以编程方式添加一个标签的约束 [英] One label's constraints added using IB, one label's constraints added programmatically

查看:79
本文介绍了使用IB添加一个标签的约束,以编程方式添加一个标签的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将两个标签彼此重叠放置.我将标签拖到情节提要板上,将标签A固定在距顶部的任意距离处,然后将标签A在视图中水平居中,从而产生蓝色约束.

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.

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

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
    }

}

但是,当我运行我的应用程序时,iOS打破了一些限制,我看到的是两个标签与顶部等距,但水平彼此相邻.另外,labelB将labelA下拉到其y位置.我想看到的是labelA停留在我最初对其进行约束的位置,并且labelB位于labelA的顶部.

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.

是否可以使用IB设置一个子视图的约束,并使用代码设置另一个子视图的约束?

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

输出:

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.

推荐答案

好吧,我读了一个有关

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

它的工作方式相同.

任何人都可以解释我要消除的约束吗?谢谢.

Can anyone explain what constraints I am removing? Thanks.

这篇关于使用IB添加一个标签的约束,以编程方式添加一个标签的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆