iPad Landscape and Portrait different layouts with Size Class iPad Landscape and Portrait different layouts with Size Class ios ios

iPad Landscape and Portrait different layouts with Size Class


Finally I found a solution :

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


It appears to be Apple's intent to treat both iPad orientations as the same -- but as a number of us are finding, there are very legitimate design reasons to want to vary the UI layout for iPad Portrait vs. iPad Landscape.

However, please see this answer for another approach to adapting size classes to do what we need:https://stackoverflow.com/a/28268200/4517929


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