答案 0 :(得分:1)
您不能仅在界面生成器中处理这种情况。我建议您使用UIStackView
并在代码中更改方向时更改其轴。
也许您还想为红色视图设置和激活/禁用一些宽度和高度限制,以根据当前方向保持其正确大小。
解决方案如下:
@IBOutlet weak var outerStackView: UIStackView!
@IBOutlet weak var redViewWidthConstraint: NSLayoutConstraint!
@IBOutlet weak var redViewHeightConstraint: NSLayoutConstraint!
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let willTransitionToLandscape = (size.width > size.height)
if willTransitionToLandscape {
redViewHeightConstraint.isActive = false
redViewWidthConstraint.isActive = true
} else {
redViewWidthConstraint.isActive = false
redViewHeightConstraint.isActive = true
}
coordinator.animate(alongsideTransition: { _ in
self.outerStackView.axis = willTransitionToLandscape ? .horizontal : .vertical
self.view.layoutIfNeeded()
})
}