簡體   English   中英

如何在情節提要中以iPad橫向和縱向模式將不同的幀設置為同一視圖?

[英]How to set different frame to same view fo iPad landscape and portrait mode in storyboard?

對於iPad,橫向和縱向模式的特征約束相同。 兩者的寬度和高度都是規則的嗎?

如何為兩種模式分別設置約束,我需要如下圖所示的布局:

在此處輸入圖片說明

橫向模式:

在此處輸入圖片說明

您不能僅在界面生成器中處理這種情況。 我建議您使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM