swift – The best way to lock orientation of 1 view controller to portrait mode solely?

0
33
swift – The best way to lock orientation of 1 view controller to portrait mode solely?


As of iOS 10 and 11, iPad helps Slide Over and Break up View. To allow an app in Slide Over and Break up View, Requires full display screen should be unchecked. Meaning the accepted reply can’t be used if the app desires to help Slide Over and Break up View. See extra from Apple’s Adopting Multitasking Enhancements on iPad right here.

I’ve an answer that enables (1) unchecking Requires full display screen, (2) only one perform to be applied in appDelegate (particularly if you happen to do not need to / cannot modify the goal view controllers), and (3) keep away from recursive calls. No want of helper class nor extensions.

appDelegate.swift (Swift 4)

func software(_ software: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    // Seek for the seen view controller
    var vc = window?.rootViewController
    // Dig by means of tab bar and navigation, regardless their order 
    whereas (vc is UITabBarController) || (vc is UINavigationController) {
        if let c = vc as? UINavigationController {
            vc = c.topViewController
        } else if let c = vc as? UITabBarController {
            vc = c.selectedViewController
        }
    }
    // Search for mannequin view controller
    whereas (vc?.presentedViewController) != nil {
        vc = vc!.presentedViewController
    }
    print("vc = " + (vc != nil ? String(describing: sort(of: vc!)) : "nil"))
    // Closing test if it is our goal class.  Additionally ensure that it is not exiting.
    // In any other case, system will mistakenly rotate the presentingViewController.
    if (vc is TargetViewController) && !(vc!.isBeingDismissed) {
        return [.portrait]
    }
    return [.all]
}

Edit

@bmjohns identified that this perform will not be referred to as on iPad. I verified and sure it was not referred to as. So, I did a bit extra testing and came upon some information:

  1. I unchecked Requires full display screen as a result of I need to allow Slide Over and Slide View on iPad. That requires the app to help all 4 orientation for iPad, in Information.plist: Supported interface orientations (iPad).

My app works similar approach as Fb: on iPhone, more often than not it’s locked to portrait. When viewing picture in full display screen, permits customers to rotate panorama for higher view. On iPad, customers can rotate to any orientation in any view controllers. So, the app seems to be good when iPad is stand on Sensible Cowl (panorama left).

  1. For iPad to name software(_:supportedInterfaceOrientationsFor), in Information.plist, solely maintain portrait for iPad. The app will lose Slide Over + Break up View capacity. However you may lock or unlock the orientation for any view controller, in only one place and no want to change ViewController class.

  2. Lastly, this perform get referred to as on view controller’s life cycle, when view is displayed/eliminated. In case your app have to lock/unlock/change orientation in different time, it won’t work

LEAVE A REPLY

Please enter your comment!
Please enter your name here