gpt4 book ai didi

ios - 使用自动布局以编程方式将 UIView 居中会使 View 从 super View 中消失

转载 作者:可可西里 更新时间:2023-11-01 00:23:18 25 4
gpt4 key购买 nike

我有以下代码使用 AutoLayout 简单地将红色方 block 居中在我的 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
}
}

然而,结果屏幕只显示蓝色背景,没有红色方 block 的迹象......即使在 Xcode 中使用查看调试功能也看不到它。

enter image description here

如果我注释掉 setupConstraints() ,它与我在初始化期间给正方形的原始框架一样“预期”。

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

广场不见了,怎么会这样?

更新:当我添加 width 时问题仍然存在和 height @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)
}

最佳答案

尝试将您的 setupConstraints 函数更改为:

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

}

关于ios - 使用自动布局以编程方式将 UIView 居中会使 View 从 super View 中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34161709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com