如何以编程方式添加NSLayoutConstraints

Vik*_*kas 2 ios nslayoutconstraint programmatically-created

我想添加一个按钮作为根视图的子视图.按钮必须水平放置在中心以及垂直于superview的中心.我以编程方式使用NSLayoutConstraints.这是我的代码.

import UIKit

class ViewController: UIViewController {

    var button : UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {

        button = UIButton()
        button.setTitle("Hello World", forState: .Normal)
        button.backgroundColor = UIColor.blueColor()
        button.sizeToFit()
        self.view.addSubview(button)

        NSLayoutConstraint.activateConstraints([

            NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: button, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1, constant: 0)
            ])

    }
}
Run Code Online (Sandbox Code Playgroud)

我收到了警告信息,也没有得到预期的结果.

2016-07-20 11:01:10.187 build [26982:309535]无法同时满足约束条件.可能至少下列列表中的一个约束是您不想要的约束.

试试这个:

(1)查看每个约束并试图找出你不期望的;

(2)找到添加了不需要的约束或约束的代码并修复它.(注意:如果你看到你不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性translatesAutoresizingMaskIntoConstraints的文档)("","","","")

将尝试通过打破约束来恢复

在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以便在调试器中捕获它.在列出的UIView上的UIConstraintBasedLayoutDebugging类别中的方法也可能有所帮助.2016-07-20 11:01:10.188 build [26982:309535]无法同时满足约束条件.可能至少下列列表中的一个约束是您不想要的约束.

试试这个:

(1)查看每个约束并试图找出你不期望的;

(2)找到添加了不需要的约束或约束的代码并修复它.(注意:如果你看到你不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性translatesAutoresizingMaskIntoConstraints的文档)("","","","")

将尝试通过打破约束来恢复

在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以便在调试器中捕获它.在列出的UIView上的UIConstraintBasedLayoutDebugging类别中的方法也可能有所帮助.

小智 8

NSLayoutConstraint是用于动态添加自动布局约束的方法之一.

 let new_view:UIView! = UIView(frame: CGRectMake(20, 20, 100, 100));
            new_view.backgroundColor = UIColor.redColor();
            new_view.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(new_view);
            NSLayoutConstraint(item: new_view,
                attribute: .Leading,
                relatedBy: .Equal,
                toItem: view,
                attribute: .LeadingMargin,
                multiplier: 1.0,
                constant: 0.0).active = true
            NSLayoutConstraint(item: new_view,
                attribute: .Top,
                relatedBy: .Equal,
                toItem: view,
                attribute: .TopMargin,
                multiplier: 1.0,
                constant: 20.0).active = true
            NSLayoutConstraint(item: new_view,
                attribute: .Height,
                relatedBy: .Equal,
                toItem: new_view,
                attribute:.Width,
                multiplier: 2.0,
                constant:0.0).active = true
            NSLayoutConstraint(item: new_view,
                attribute: .Width,
                relatedBy: .Equal,
                toItem: nil,
                attribute: .NotAnAttribute,
                multiplier: 1.0,
                constant: 100.0).active = true
Run Code Online (Sandbox Code Playgroud)

将translatesAutoresizingMaskIntoConstraints设置为false并调用layoutifneeded方法.

  • @Vikas接受答案,如果它适合你. (2认同)