how to lock portrait orientation for only main view using swift

后端 未结 16 1861
野性不改
野性不改 2020-12-07 13:38

I have created an application for iPhone, using swift, that is composed from many views embedded in a navigation controller. I would like to lock the main v

相关标签:
16条回答
  • 2020-12-07 13:57

    When UIKit detects a change in device orientation, it uses the UIApplication object and the root view controller to determine whether the new orientation is allowed. If both objects agree that the new orientation is supported, then auto-rotation occurs. Otherwise, the orientation change is ignored.

    By default, the UIApplication object sources its supported interface orientations from the values specified for the UISupportedInterfaceOrientations key in the applications' Information Property List. You can override this behavior by implementing the application:supportedInterfaceOrientationsForWindow: method in your application's delegate. The supported orientation values returned by this method only take effect after the application has finished launching. You can, therefore, use this method to support a different set of orientations after launch.

    Allowing your app to rotate into portrait after launch.

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    

    source: https://developer.apple.com/

    0 讨论(0)
  • 2020-12-07 13:58

    In iOS8, if you want to lock some especific ViewController, create an extension of UINavigationController (in the case you use it):

    extension UINavigationController {
    public override func shouldAutorotate() -> Bool {
    if visibleViewController is YourViewController {
        return false
    }
    return true
    }}
    
    0 讨论(0)
  • 2020-12-07 14:02

    If your view is embedded in navigationcontroller in storyboard set the navigation controller delegate UINavigationControllerDelegate and add the following method

    class ViewController: UIViewController, UINavigationControllerDelegate {
        func navigationControllerSupportedInterfaceOrientations(navigationController: UINavigationController) -> UIInterfaceOrientationMask {
            return UIInterfaceOrientationMask.Portrait
        }
    }
    
    0 讨论(0)
  • 2020-12-07 14:03

    The important think is to describe supported interface orientations for whole application in AppDelegate. For example to block all views to portrait just do this:

      func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask{
        return UIInterfaceOrientationMask.portrait
    }
    

    Lots of people are looking just for this answer by googling threw this question so I hope you can excuse me.

    Works in Swift 3.0

    0 讨论(0)
  • 2020-12-07 14:05

    According to the Swift Apple Docs for supportedInterfaceOrientations:

    Discussion

    When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller's shouldAutorotate method returns true.

    Your navigation controller should override shouldAutorotate and supportedInterfaceOrientations as shown below. I did this in a UINavigationController extension for ease:

    extension UINavigationController {
        public override func shouldAutorotate() -> Bool {
            return true
        }
    
        public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
            return (visibleViewController?.supportedInterfaceOrientations())!
        }
    }
    

    And your main viewcontroller (portrait at all times), should have:

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }
    

    Then, in your subviewcontrollers that you want to support portrait or landscape:

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.All
    }
    

    Edit: Updated for iOS 9 :-)

    0 讨论(0)
  • 2020-12-07 14:05

    Edited for swift2.0 :

    override func shouldAutorotate() -> Bool {
            return false
    }
    
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return [.Portrait, .PortraitUpsideDown]
    }
    
    0 讨论(0)
提交回复
热议问题