使用Autolay以编程方式居中放置UIView将使视图从超级视图中消失
我有下面的代码来简单地在我的ViewController
的view
使用AutoLayout
约束以编程方式居中红色正方形:
class ViewController: UIViewController { let square: UIView required init?(coder aDecoder: NSCoder) { let squareFrame = CGRectMake(0.0, 0.0, 500.0, 500.0) self.square = UIView(frame: squareFrame) super.init(coder: aDecoder) } override func viewDidLoad() { self.view.addSubview(self.square) self.square.backgroundColor = UIColor.redColor() self.view.backgroundColor = UIColor.blueColor() print(self.square) setupConstraints() print(self.square) } func setupConstraints() { self.square.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.square, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0).active = true NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.square, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0).active = true } }
然而,结果屏幕只显示蓝色背景,没有红色的正方形。即使在Xcode中使用视图debuggingfunction也无法看到。
如果我注释掉setupConstraints()
,它就像在初始化期间我给出的原始帧一样“预期”。
顺便说一下,这两个print
语句具有完全相同的输出: <UIView: 0x7ff1c8d3f3e0; frame = (0 0; 500 500); layer = <CALayer: 0x7ff1c8d04c00>>
<UIView: 0x7ff1c8d3f3e0; frame = (0 0; 500 500); layer = <CALayer: 0x7ff1c8d04c00>>
当广场无处可见时,这怎么可能呢?
更新 :问题仍然存在,当我添加width
和height
约束build议由@HaydenHolligan setupConstraints()
:
func setupConstraints() { self.square.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.square, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0).active = true NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.square, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0).active = true // the following lines have no effect with respect to the issue mentioned aboove let sizeFormat = "[square(100@100)]" let size = NSLayoutConstraint.constraintsWithVisualFormat(sizeFormat, options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["square": self.square]) self.view.addConstraints(size) }
尝试改变你的setupConstraintsfunction:
func setupConstraints() { self.square.translatesAutoresizingMaskIntoConstraints = false let centerX = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,toItem: self.square, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0) let centerY = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal,toItem: self.square, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0) let squareWidth = NSLayoutConstraint(item: self.square, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:500) let squareHeight = NSLayoutConstraint(item: self.square, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:500) self.view.addConstraints([centerX , centerY ,squareWidth , squareHeight]) }