使用Autolayout以编程方式居中UIView使视图从superview中消失

时间:2015-12-08 17:00:30

标签: ios swift autolayout ios-autolayout

我有以下代码,只需在我的AutoLayout ViewController view中以编程方式使用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 } } 约束将红色方块居中:

setupConstraints()

结果屏幕只显示蓝色背景,没有红色方块的标志......即使在Xcode中使用查看调试功能,也无法看到。

enter image description here

如果我发表评论print,那么它的作用就是"期待"在初始化期间我给了正方形的原始框架。

顺便说一下,两个<UIView: 0x7ff1c8d3f3e0; frame = (0 0; 500 500); layer = <CALayer: 0x7ff1c8d04c00>>语句都具有完全相同的输出: width

当广场无处可见时,怎么会这样呢?

更新: 当@HaydenHolligan在height中建议添加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) } 约束时,问题仍然存在:

{{1}}

1 个答案:

答案 0 :(得分:6)

尝试将setupConstraints func更改为:

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

}