如何在使用constraintEqualToAnchor()设置它们后更改自动布局约束?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在使用constraintEqualToAnchor()设置它们后更改自动布局约束?相关的知识,希望对你有一定的参考价值。
我尝试使用constraintEqualToAnchor()
设置具有AutoLayout约束的视图:
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
}
在最后一行代码中,我尝试更改其中一个约束。我认为它会工作,但它在控制台日志中给出了一些错误
"<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)]>"
当使用constraintEqualToAnchor()?
时,在我设置它们之后更改约束的正确方法是什么?
答案
您需要在激活新约束时停用先前的约束,这样您才不会过度约束视图。为此,将每个约束的引用存储为ViewController
中的属性,然后在创建和激活新约束之前将旧约束的active
属性设置为false
:
Swift 2.x:
class ViewController: UIViewController {
var leftConstraint: NSLayoutConstraint?
var trailingConstraint: NSLayoutConstraint?
var topConstraint: NSLayoutConstraint?
var bottomConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
let myView = UIView()
myView.backgroundColor = UIColor.orangeColor()
myView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(myView)
leftConstraint = myView.leftAnchor.constraintEqualToAnchor(view.leftAnchor)
leftConstraint?.active = true
trailingConstraint = myView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor)
trailingConstraint?.active = true
topConstraint = myView.topAnchor.constraintEqualToAnchor(view.topAnchor)
topConstraint?.active = true
bottomConstraint = myView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor)
bottomConstraint?.active = true
/******************************************/
/* I try to change one of the constraints */
/******************************************/
leftConstraint?.active = false
leftConstraint = myView.leftAnchor.constraintEqualToAnchor(view.rightAnchor, constant: -100)
leftConstraint?.active = true
}
}
更新Swift 3语法:
class ViewController: UIViewController {
var leftConstraint: NSLayoutConstraint?
var trailingConstraint: NSLayoutConstraint?
var topConstraint: NSLayoutConstraint?
var bottomConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
let myView = UIView()
myView.backgroundColor = UIColor.orange
myView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(myView)
leftConstraint = myView.leftAnchor.constraint(equalTo: view.leftAnchor)
leftConstraint?.isActive = true
trailingConstraint = myView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
trailingConstraint?.isActive = true
topConstraint = myView.topAnchor.constraint(equalTo: view.topAnchor)
topConstraint?.isActive = true
bottomConstraint = myView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
bottomConstraint?.isActive = true
/******************************************/
/* I try to change one of the constraints */
/******************************************/
leftConstraint?.isActive = false
leftConstraint = myView.leftAnchor.constraint(equalTo: view.rightAnchor, constant: -100)
leftConstraint?.isActive = true
}
}
另一答案
下面是一个声明约束c
的示例,该约束将在稍后的时间引用。我们设置一个新的常量值,然后在superview上调用layout
。
myView.translatesAutoresizingMaskIntoConstraints = false
var constraints: [NSLayoutConstraint] = [
myView.topAnchor.constraintEqualToAnchor(view.topAnchor),
myView.leftAnchor.constraintEqualToAnchor(view.leftAnchor),
myView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor)
]
let c = myView.rightAnchor.constraintEqualToAnchor(view.rightAnchor)
constraints.append(c)
view.addSubview(myView)
NSLayoutConstraint.activateConstraints(constraints)
// Some time later
c.constant = -100
view.setNeedsLayout()
另一答案
// method myView.topAnchor.constraintEqualToAnchor creates new one inactive anchor
// and not returns exist with equal relationships
// you can set identifier for any constraint in Interface Builder or code and find & update in code
for ( NSLayoutConstraint *c in [self.view constraintsAffectingLayoutForAxis:UILayoutConstraintAxisHorizontal] )
{
if ( YES == [c.identifier isEqualToString:@"my.layout-constraint.id"] )
{
// Unlike the other properties, the constant can be modified
// after constraint creation.
// Setting the constant on an existing constraint performs much better
// than removing the constraint and adding a new one that's exactly like
// the old except that it has a different constant.
c.constant = 123;
// if needed
// [self.view setNeedsUpdateConstraints];
break;
}
}
另一答案
以下是IF语句在分段点击时修改StackView和View的示例:
if (sender as AnyObject).selectedSegmentIndex == 0{
// Shrink the white view and stack view
heightConstraintView = containerView.heightAnchor.constraint(equalToConstant: 100)
heightConstraintView?.isActive = true
heightConstraintStackView = stackView.heightAnchor.constraint(equalToConstant: 100)
heightConstraintStackView?.isActive = true
}
else {
// Before returning back the white view and stack view DEACTIVATE teh previous constraints
heightConstraintView?.isActive = false
heightConstraintStackView?.isActive = false
// Returning back the white view and stack view to normal size
heightConstraintView = containerView.heightAnchor.constraint(equalToConstant: 200)
heightConstraintView?.isActive = true
heightConstraintStackView = stackView.heightAnchor.constraint(equalToConstant: 200)
heightConstraintStackView?.isActive = true
}
以上是关于如何在使用constraintEqualToAnchor()设置它们后更改自动布局约束?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Firebase 在 Web 上托管 Flutter?它的效果如何?
如何在自动布局中使用约束标识符以及如何使用标识符更改约束? [迅速]
如何使用 Java 在 selenium webdriver 中打开新选项卡,或者如何使用 selenium webdriver 使用动作类在 selenium 中按 ctrl + T [重复]