以编程方式自动布局(NSLayoutConstraint):在外部视图中居中内部视图并限制为最小(宽度,高度)
Auto Layout programmatically (NSLayoutConstraint): center inner view in outer view and constrain to min(width, height)
我不确定我在使用 NSLayoutConstraint 生成自动布局时做错了什么。
我想使用外部视图的宽度和高度中较小的一个,将内部视图置于外部视图的中心。最后,我想应用一个比例因子。
这是我的代码。
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
约束有点奇怪。只设置一次。
此外,您需要 select 内部视图尺寸的外部锚点基于实际外部视图的框架。如果最小的外部尺寸是宽度,那么你将内部视图的宽度限制为 outer.widthAnchor * imageScale
。否则,当最小外部尺寸为高度时,您将约束为 outer.heightAnchor * imageScale
.
这对我来说很有效,可以得到你正在寻找的布局(我刚刚创建了一个简单的单视图项目):
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)
])
}
}
我不确定我在使用 NSLayoutConstraint 生成自动布局时做错了什么。
我想使用外部视图的宽度和高度中较小的一个,将内部视图置于外部视图的中心。最后,我想应用一个比例因子。
这是我的代码。
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
约束有点奇怪。只设置一次。
此外,您需要 select 内部视图尺寸的外部锚点基于实际外部视图的框架。如果最小的外部尺寸是宽度,那么你将内部视图的宽度限制为 outer.widthAnchor * imageScale
。否则,当最小外部尺寸为高度时,您将约束为 outer.heightAnchor * imageScale
.
这对我来说很有效,可以得到你正在寻找的布局(我刚刚创建了一个简单的单视图项目):
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)
])
}
}