Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式添加子视图并使用约束对其进行定位_Ios_Swift_Uiview_Uilabel_Autolayout - Fatal编程技术网

Ios 以编程方式添加子视图并使用约束对其进行定位

Ios 以编程方式添加子视图并使用约束对其进行定位,ios,swift,uiview,uilabel,autolayout,Ios,Swift,Uiview,Uilabel,Autolayout,我已使用IB on向视图控制器添加了一个UIView。让我们称之为redView 然后,我在代码中使用自动布局约束固定了它的所有四边,当我运行它时,它看起来就像预期的那样 现在,我想以编程方式将UILabel添加到此视图,并使用自动布局约束将其放置在中心位置 下面是我到目前为止的代码 import UIKit class ViewController: UIViewController { @IBOutlet private var redView: UIView! o

我已使用IB on向视图控制器添加了一个
UIView
。让我们称之为
redView

然后,我在代码中使用自动布局约束固定了它的所有四边,当我运行它时,它看起来就像预期的那样

现在,我想以编程方式将
UILabel
添加到此视图,并使用自动布局约束将其放置在中心位置

下面是我到目前为止的代码

import UIKit

class ViewController: UIViewController {

    @IBOutlet private var redView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        redView.setTranslatesAutoresizingMaskIntoConstraints(false)

        let leadingConstraint = NSLayoutConstraint(item: redView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1, constant: 0)
        let trailingConstraint = NSLayoutConstraint(item: redView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1, constant: 0)
        let topConstraint = NSLayoutConstraint(item: redView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0)
        let bottomConstraint = NSLayoutConstraint(item: redView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1, constant: 0)
        view.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])


        let label = UILabel()
        label.text = "Auto Layout Exercise"
        redView.addSubview(label)

        let xCenterConstraint = NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: redView, attribute: .CenterX, multiplier: 1, constant: 0)
        let yCenterConstraint = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: redView, attribute: .CenterY, multiplier: 1, constant: 0)
        let leadingConstraint1 = NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: redView, attribute: .Leading, multiplier: 1, constant: 20)
        let trailingConstraint1 = NSLayoutConstraint(item: label, attribute: .Trailing, relatedBy: .Equal, toItem: redView, attribute: .Trailing, multiplier: 1, constant: -20)
        redView.addConstraints([xCenterConstraint, yCenterConstraint, leadingConstraint1, trailingConstraint1])

    }

}
问题是标签没有出现在
红色视图上。有人能告诉我我错过了什么吗


谢谢。

我很确定您还需要设置:

label.setTranslatesAutoresizingMaskIntoConstraints(false)

否则,我不认为约束将应用于标签

Swift 2/3的注意事项

它已更改为布尔属性:

而不是以前的:

label.setTranslatesAutoresizingMaskIntoConstraints(false)
现在是:

label.translatesAutoresizingMaskIntoConstraints = false

您需要对每个UIControl执行此操作以分配NSConstraints

label.translatesAutoresizingMaskIntoConstraints = false

事实上,您应该在控制台中看到这样的消息。感谢上帝,控制台中有这样的消息,因为我几乎可以保证每次都会忘记这行代码。:)是的,这就是原因。我曾经用
redView
setTranslatesAutoResizengMacSkintoConstraints
行进行过测试,但这并不影响视图,所以我认为这行不是强制性的。我想这是针对以编程方式创建的视图。谢谢