Locking Portrait Orientation in View? iOS 7

2019-03-19 02:52发布

问题:

So, I want to lock the orientation of my home page to portrait, and the Home page ONLY.

I am using a tab bar controller, so the initial view is the tab controller, but the view controller that appears first is the first tab, e.g. the Home page.

I would like to make it so that when the user goes to rotate the device, it WILL NOT rotate to landscape on this page. However all other pages can rotate.

I have searched around, and nothing seems to be specific to iOS 7, and the one that is specific to iOS 7 doesn't work…

Please help, thank you!

The image below describes what I DON"T want to happen, for this page.

回答1:

Implement the following in your implementation

- (NSUInteger) supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskPortrait;

}

This should give you the results you are looking for!



回答2:

Use this code

@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{

    return UIInterfaceOrientationMaskPortrait;

}

-(NSUInteger)supportedInterfaceOrientations
{

  return UIInterfaceOrientationMaskPortrait;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{

 return UIInterfaceOrientationPortrait;

}

@end


回答3:

The problem is, as you've rightly pointed out, that your home tab is not the topmost view controller.

From my limited knowledge on the subject I can only think of the following:

  1. Create another tab view controller and implement the methods to control orientation, i.e. shouldAutorotate and supportedInterfaceOrientations;
  2. Make this controller the first one at startup;
  3. Route the other tabs down to the original tab controller (the one that supports all orientations) using a push segue.


回答4:

I think I found a nice solution. Well, in my case I'm using a UISplitViewController as rootController in the storyboard but the idea is the same.

  1. SubClass your rootController (In my case UISplitViewController) and Catch the shouldAutorotate() callback so you can call subviews shouldAutorotate from there.
  2. Implement shouldAutorotate() in the View you want to Lock the Rotation

    class MyUISplitViewController: UISplitViewController {
    override func shouldAutorotate() -> Bool {
        if ((self.viewControllers.last) != nil && (self.viewControllers.last!.topViewController) != nil){
            if (self.viewControllers.last!.topViewController!.respondsToSelector("shouldAutorotate"))
            {
                return self.viewControllers.last!.topViewController!.shouldAutorotate()
            }
        }
        return true
    }
    }
    

In your sub UIViewController

override func shouldAutorotate() -> Bool {
        if (UIDevice.currentDevice().userInterfaceIdiom == .Phone)
        {
            return false
        }else{
            return true
        }
    }

If you want to check the supported orientations, you can simply do the same with supportedsupportedInterfaceOrientations()

EDIT:

Don't forget to set your "MyUISplitViewController" class in your Storyboard root viewController