我不确定我在使用 NSLayoutConstraint 生成自动布局时做错了什么。
我想使用外部 View 的宽度和高度中较小的一个,将内部 View 置于外部 View 的中心。最后,我想应用一个比例因子。
这是我的代码。
NSLayoutConstraint.activate([
inner.centerXAnchor.constraint(equalTo: outer.centerXAnchor),
inner.centerYAnchor.constraint(equalTo: outer.centerYAnchor),
inner.widthAnchor.constraint(equalTo: outer.heightAnchor, multiplier: imageScale),
inner.widthAnchor.constraint(equalTo: outer.widthAnchor, multiplier: imageScale),
inner.heightAnchor.constraint(equalTo: inner.widthAnchor)
])
这适用于:
- 宽度==高度
- 宽度<高度
但是当 width > height 我得到这个:
我错过了什么?使用框架很容易做到这一点:
let innerWidth = min(outer.frame.width, outer.frame.height) * imageScale
let innerHeight = innerWidth
inner.frame = CGRect(x: (outer.frame.width -innerWidth) / 2,
y: (outer.frame.height - innerHeight) / 2,
width: innerWidth, height: innerHeight)
最佳答案
首先,您在代码中两次设置 inner.widthAnchor
约束有点奇怪。仅设置一次。
此外,您需要根据实际外部 View 的框架为内部 View 的尺寸选择外部 anchor 。如果最小的外部尺寸是宽度,则将内部 View 的宽度限制为 outer.widthAnchor * imageScale
。否则,当最小外部尺寸为高度时,您将限制为 outer.heightAnchor * imageScale
。
这对我来说很有效,可以得到你正在寻找的布局(我刚刚创建了一个简单的单 View 项目):
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let inner = UIView()
inner.backgroundColor = .yellow
inner.translatesAutoresizingMaskIntoConstraints = false
let outer = UIView()
outer.backgroundColor = .blue
outer.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(outer)
view.addSubview(inner)
// get these from somewhere, e.g. outerWidth.frame.size
let outerWidth = CGFloat(200)
let outerHeight = CGFloat(400)
let outerAnchor: NSLayoutDimension
if outerWidth >= outerHeight {
outerAnchor = outer.heightAnchor
} else {
outerAnchor = outer.widthAnchor
}
let imageScale = CGFloat(0.5)
NSLayoutConstraint.activate([
outer.widthAnchor.constraint(equalToConstant: outerWidth),
outer.heightAnchor.constraint(equalToConstant: outerHeight),
outer.centerXAnchor.constraint(equalTo: view.centerXAnchor),
outer.centerYAnchor.constraint(equalTo: view.centerYAnchor),
inner.centerXAnchor.constraint(equalTo: outer.centerXAnchor),
inner.centerYAnchor.constraint(equalTo: outer.centerYAnchor),
inner.widthAnchor.constraint(equalTo: outerAnchor, multiplier: imageScale),
inner.heightAnchor.constraint(equalTo: inner.widthAnchor)
])
}
}
关于swift - 以编程方式自动布局(NSLayoutConstraint): center inner view in outer view and constrain to min(width, 高度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43011289/