如何使用子类中的NSLayoutConstraints (或锚点)编写以下代码
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,谢谢。
发布于 2016-11-25 10:23:41
检查以下代码:
let view = UIView(frame: CGRectZero)
view.setTranslatesAutoresizingMaskIntoConstraints(false)
super.init(frame: frame)
let viewsDict = ["view": view]
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-0-|", options: .allZeros, metrics: nil, views: viewsDict))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: .allZeros, metrics: nil, views: viewsDict))
addSubview(view)发布于 2016-11-29 06:43:30
我通过size类复制了相同的行为,但只针对iPhones。看看这个故事板。(这不是代码,是你要的)
由于iPads具有宽度:正则和高度:规则,所以iPads似乎不可能不使用layoutSubview。
https://stackoverflow.com/questions/40802240
复制相似问题