How to lock orientation of one view controller to portrait mode only in Swift

后端 未结 16 2057
梦谈多话
梦谈多话 2020-11-22 12:07

Since my app got support for all orientation. I would like to lock only portrait mode to specific UIViewController.

e.g. assume it was Tabbed Application and when Si

相关标签:
16条回答
  • 2020-11-22 12:40

    Create new extension with

    import UIKit
    
    extension UINavigationController {
        override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return .portrait
        }
    }
    
    extension UITabBarController {
        override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return .portrait
        }
    }
    
    0 讨论(0)
  • 2020-11-22 12:40

    I experimented a little bit and I managed to find clean solution for this problem. The approach is based on the view tagging via view->tag

    In the target ViewController just assign the tag to the root view like in the following code example:

    class MyViewController: BaseViewController {
    
      // declare unique view tag identifier
      static let ViewTag = 2105981;
    
      override func viewDidLoad()
      {
        super.viewDidLoad();
        // assign the value to the current root view
        self.view.tag = MyViewController.ViewTag;
      }
    

    And finally in the AppDelegate.swift check if the currently shown view is the one we tagged:

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
    {
        if (window?.viewWithTag(DesignerController.ViewTag)) != nil {
            return .portrait;
        }
        return .all;
    }
    

    This approach has been tested with my simulator and seems it works fine.

    Note: the marked view will be also found if current MVC is overlapped with some child ViewController in navigation stack.

    0 讨论(0)
  • 2020-11-22 12:42

    Here is a simple way that works for me with Swift 4.2 (iOS 12.2), put this in a UIViewController for which you want to disable shouldAutorotate:

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    

    The .portrait part tells it in which orientation(s) to remain, you can change this as you like. Choices are: .portrait, .all, .allButUpsideDown, .landscape, .landscapeLeft, .landscapeRight, .portraitUpsideDown.

    0 讨论(0)
  • 2020-11-22 12:43

    Thanks to @bmjohn's answer above. Here is a working Xamarin / C# version of the that answer's code, to save others the time of transcription:

    AppDelegate.cs

     public UIInterfaceOrientationMask OrientationLock = UIInterfaceOrientationMask.All;
     public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
     {
         return this.OrientationLock;
     }
    

    Static OrientationUtility.cs class:

    public static class OrientationUtility
    {
        public static void LockOrientation(UIInterfaceOrientationMask orientation)
        {
            var appdelegate = (AppDelegate) UIApplication.SharedApplication.Delegate;
            if(appdelegate != null)
            {
                appdelegate.OrientationLock = orientation;
            }
        }
    
        public static void LockOrientation(UIInterfaceOrientationMask orientation, UIInterfaceOrientation RotateToOrientation)
        {
            LockOrientation(orientation);
    
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)RotateToOrientation), new NSString("orientation"));
        }
    }
    

    View Controller:

        public override void ViewDidAppear(bool animated)
        {
           base.ViewWillAppear(animated);
           OrientationUtility.LockOrientation(UIInterfaceOrientationMask.Portrait, UIInterfaceOrientation.Portrait);
        }
    
        public override void ViewWillDisappear(bool animated)
        {
            base.ViewWillDisappear(animated);
            OrientationUtility.LockOrientation(UIInterfaceOrientationMask.All);
        }
    
    0 讨论(0)
提交回复
热议问题