iPad Landscape and Portrait different layouts with Size Class

后端 未结 4 1259

How to design iPad Landscape and Portrait screens with different Layouts using Size class. I could find only w-regular and h-regular for both orientations. Example: I need t

4条回答
  •  忘掉有多难
    2021-01-01 11:16

    For Swift 3 it would look like this:

    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)
    }
    

    It will use wRhC instead of wRhR for iPad devices in landscape mode. Put this code to your base view controller, i.e. this rule will work for all controllers that were presented by this one. You can put any additional conditions here... For example, if you want this rule to be working only for specific view controller, your if operator would look like this:

        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)
    
        }
    

提交回复
热议问题