[英]How to set different frame to same view fo iPad landscape and portrait mode in storyboard?
您不能僅在界面生成器中處理這種情況。 我建議您使用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()
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.