ios - 如何以编程方式添加 NSLayoutConstraints

标签 ios nslayoutconstraint programmatically-created

我想添加一个按钮作为 Root View 的 subview 。按钮必须水平放置在父 View 的中心,垂直放置在父 View 的中心。我以编程方式使用 NSLayoutConstraints。这是我的代码。

class ViewController: UIViewController {

    var button: UIButton!

    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)
        ])
    }
}

我收到以下警告消息,而且我没有得到想要的结果。

2016-07-20 11:01:10.187 build[26982:309535] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", " (Names: '|':UIViewControllerWrapperView:0x7faa4be31770 )>", "" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 2016-07-20 11:01:10.188 build[26982:309535] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", "", " (Names: '|':UIViewControllerWrapperView:0x7faa4be31770 )>" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

最佳答案

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

设置translatesAutoresizingMaskIntoConstraintsfalse并调用layoutifneeded方法。

关于ios - 如何以编程方式添加 NSLayoutConstraints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38473352/

相关文章:

ios - map 设备倾斜到物理世界重力?

ios - 核心数据上下文不保存

c# - 尝试以编程方式运行容器时从 Docker 退出代码 125

ios - 知道何时在 Apple Watch 模态界面 Controller 中单击取消按钮

ios - 拖放的自定义 View

ios - View 宽度无法通过自动布局正确更改

ios - 为什么当我相对于 super View 的底部进行约束时,它是从 super View 的顶部进行操作的?现在是这样吗?

带有自动布局的 UICollectionViewCell 动态高度

android - 以编程方式在 ScrollView 中添加一个 View

swift - 如何在 Master 和 Detail View 之间 segue *without* storyboard?