【发布时间】:2016-11-25 10:02:18
【问题描述】:
我如何在 UIView 子类中使用 NSLayoutConstraints(或 anchors)编写以下代码?
func commonInit() {
aView = UIView()
aView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
addSubview(aView)
}
override func layoutSubviews() {
super.layoutSubviews()
let smallerSide = min(bounds.width, bounds.height)
aView.bounds = CGRect(x: 0.0, y: 0.0, width: smallerSide, height: smallerSide)
aView.center = CGPoint(x: bounds.midX, y: bounds.midY)
}
一个目标是避免使用layoutSubviews()。
此外,aView 必须保持 1:1 的纵横比。
如果您需要更多信息,请告诉我。
PS:请让我们使用swift3,谢谢。
【问题讨论】:
标签: ios swift autolayout constraints