相关疑难解决方法(0)

如何在使用constraintEqualToAnchor()设置它们后更改自动布局约束?

我尝试使用以下方法设置具有AutoLayout约束的视图constraintEqualToAnchor():

override func viewDidLoad() {
    super.viewDidLoad()

    let myView = UIView()
    myView.backgroundColor = UIColor.orangeColor()
    myView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(myView)

    myView.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true
    myView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
    myView.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
    myView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor).active = true

    /******************************************/
    /* I try to change one of the constraints */
    /******************************************/
    myView.leftAnchor.constraintEqualToAnchor(view.rightAnchor, constant: -100).active = true  
}
Run Code Online (Sandbox Code Playgroud)

在最后一行代码中,我尝试更改其中一个约束.我认为它会工作,但它在控制台日志中给出了一些错误

"<NSLayoutConstraint:0x7fb53a5180d0 H:|-(0)-[UIView:0x7fb53a5190b0](LTR)   (Names: '|':UIView:0x7fb53a519240 )>",
"<NSLayoutConstraint:0x7fb53a51f660 H:[UIView:0x7fb53a519240]-(-100)-[UIView:0x7fb53a5190b0](LTR)>",
"<NSLayoutConstraint:0x7fb53a711ee0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fb53a519240(414)]>"
Run Code Online (Sandbox Code Playgroud)

使用时constraintEqualToAnchor()?,在设置约束后,更改约束的正确方法是什么?

uiview ios autolayout nslayoutconstraint swift

18
推荐指数
1
解决办法
2万
查看次数

标签 统计

autolayout ×1

ios ×1

nslayoutconstraint ×1

swift ×1

uiview ×1