更改 UIView 的转换不会影响约束
Changing UIView's transform doesn't affect constraints
在 iOS11 中更改 UIView 的变换 属性(例如缩放)不会影响绑定到此视图的约束。在 iOS 10 中,一切都按预期工作。
我有代码:
import UIKit
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
// top view
let topView = UIView()
topView.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
topView.backgroundColor = .black
view.addSubview(topView)
topView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
// bottom view
let bottomView = UIView()
bottomView.translatesAutoresizingMaskIntoConstraints = false
bottomView.backgroundColor = .black
view.addSubview(bottomView)
NSLayoutConstraint.activate([
bottomView.heightAnchor.constraint(equalToConstant: 200),
bottomView.widthAnchor.constraint(equalToConstant: 200),
bottomView.leftAnchor.constraint(equalTo: topView.leftAnchor),
bottomView.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: 8)
])
}
}
我期待什么 (iOS 10):
我有什么 (iOS 11):
如您所见,topView
已缩放,但 bottomView
布局不正确。我该如何解决?
来自 Apple 的 UIView / Transform
文档 (https://developer.apple.com/documentation/uikit/uiview/1622459-transform):
Warning
When the value of this property is anything other than the identity transform, the value in the frame property is undefined and
should be ignored.
所以,你在 iOS 10 中得到的结果恰好是你得到的,而不是正确的[=19] =]结果。
底部视图发生的情况是,在您告诉它缩放到 %50 之前,它会与顶部视图的左锚点对齐,并且它会添加 8 个像素。我会对左边的约束进行硬编码。查看 This Tutorial 以获取有关如何使用视觉格式语言执行此操作的指南。
在 iOS11 中更改 UIView 的变换 属性(例如缩放)不会影响绑定到此视图的约束。在 iOS 10 中,一切都按预期工作。
我有代码:
import UIKit
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
// top view
let topView = UIView()
topView.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
topView.backgroundColor = .black
view.addSubview(topView)
topView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
// bottom view
let bottomView = UIView()
bottomView.translatesAutoresizingMaskIntoConstraints = false
bottomView.backgroundColor = .black
view.addSubview(bottomView)
NSLayoutConstraint.activate([
bottomView.heightAnchor.constraint(equalToConstant: 200),
bottomView.widthAnchor.constraint(equalToConstant: 200),
bottomView.leftAnchor.constraint(equalTo: topView.leftAnchor),
bottomView.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: 8)
])
}
}
我期待什么 (iOS 10):
我有什么 (iOS 11):
如您所见,topView
已缩放,但 bottomView
布局不正确。我该如何解决?
来自 Apple 的 UIView / Transform
文档 (https://developer.apple.com/documentation/uikit/uiview/1622459-transform):
Warning
When the value of this property is anything other than the identity transform, the value in the frame property is undefined and should be ignored.
所以,你在 iOS 10 中得到的结果恰好是你得到的,而不是正确的[=19] =]结果。
底部视图发生的情况是,在您告诉它缩放到 %50 之前,它会与顶部视图的左锚点对齐,并且它会添加 8 个像素。我会对左边的约束进行硬编码。查看 This Tutorial 以获取有关如何使用视觉格式语言执行此操作的指南。