web-dev-qa-db-ja.com

サイズクラスでiPadの横向きと縦向きの異なるレイアウト

サイズクラスを使用して、レイアウトが異なるiPadの横画面と縦画面をデザインする方法。両方の向きでw-regularとh-regularしか見つかりませんでした。例:サイズクラスを使用して、2つのビューを縦に縦に、横に横に整列する必要があります

24

最後に私は解決策を見つけました:

if traitCollection.verticalSizeClass == .Regular && traitCollection.horizontalSizeClass == .Regular {

     var orientation:UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation;
     if orientation == UIInterfaceOrientation.LandscapeLeft || orientation == UIInterfaceOrientation.LandscapeRight {
            // orientation is landscape


     }  else {
            // orientation is portrait

     }
}
6
cmii

IPadの両方の向きを同じように扱うことはAppleの意図のように見えます-しかし私たちの多くが見つけているように、iPadの縦向きとiPadの横向きのUIレイアウトを変更したいという非常に正当な設計上の理由があります。

ただし、サイズクラスを適応させて必要なことを行う別の方法については、この回答を参照してください。 https://stackoverflow.com/a/28268200/4517929

6
RonDiamond

Swiftの場合、次のようになります。

override func overrideTraitCollection(forChildViewController childViewController: UIViewController) -> UITraitCollection? {
    if UI_USER_INTERFACE_IDIOM() == .pad &&
        view.bounds.width > view.bounds.height {

        let collections = [UITraitCollection(horizontalSizeClass: .regular),
                           UITraitCollection(verticalSizeClass: .compact)]
        return UITraitCollection(traitsFrom: collections)

    }

    return super.overrideTraitCollection(forChildViewController: childViewController)
}

ランドスケープモードのiPadデバイスでは、wRhRの代わりにwRhCを使用します。このコードをベースビューコントローラーに配置します。つまり、このルールは、このコントローラーによって提示されたすべてのコントローラーに対して機能します。ここに追加の条件を置くことができます...たとえば、このルールを特定のビューコントローラーに対してのみ機能させる場合、if演算子は次のようになります。

    if UI_USER_INTERFACE_IDIOM() == .pad &&
        childViewController is YourSpecificViewController &&
        view.bounds.width > view.bounds.height {

        let collections = [UITraitCollection(horizontalSizeClass: .regular),
                           UITraitCollection(verticalSizeClass: .compact)]
        return UITraitCollection(traitsFrom: collections)

    }
5
Oleg

Swift 4

override func overrideTraitCollection(forChildViewController childViewController: UIViewController) -> UITraitCollection? {
    if UIDevice.current.userInterfaceIdiom == .pad && UIDevice.current.orientation.isLandscape {
        return UITraitCollection(traitsFrom:[UITraitCollection(verticalSizeClass: .compact), UITraitCollection(horizontalSizeClass: .regular)])
    }
    return super.overrideTraitCollection(forChildViewController: childViewController)
}

私は、navigationControllerのカスタムサブクラスを作成してから、ストーリーボードの初期ナビゲーションコントローラーをそのクラスに設定します。 a ViewController でも同様のことができます。

例:

import UIKit

class NavigationControllerWithTraitOverride: UINavigationController {

    // If you make a navigationController a member of this class the descendentVCs of that navigationController will have their trait collection overridden with compact vertical size class if the user is on an iPad and the device is horizontal.

    override func overrideTraitCollection(forChildViewController childViewController: UIViewController) -> UITraitCollection? {
        if UIDevice.current.userInterfaceIdiom == .pad && UIDevice.current.orientation.isLandscape {
            return UITraitCollection(traitsFrom:[UITraitCollection(verticalSizeClass: .compact), UITraitCollection(horizontalSizeClass: .regular)])
        }
        return super.overrideTraitCollection(forChildViewController: childViewController)
    }
}

注: docs のようにtraitCollectionをオーバーライドしないでください。

重要

TraitCollectionプロパティを直接使用します。オーバーライドしないでください。カスタム実装を提供しないでください。

0
DoesData