How to force a UIViewController to Portrait orientation in iOS 6

后端 未结 16 1345
别那么骄傲
别那么骄傲 2020-11-22 06:03

As the ShouldAutorotateToInterfaceOrientation is deprecated in iOS 6 and I used that to force a particular view to portrait only, what is the c

相关标签:
16条回答
  • 2020-11-22 06:37

    I disagree from @aprato answer, because the UIViewController rotation methods are declared in categories themselves, thus resulting in undefined behavior if you override then in another category. Its safer to override them in a UINavigationController (or UITabBarController) subclass

    Also, this does not cover the scenario where you push / present / pop from a Landscape view into a portrait only VC or vice-versa. To solve this tough issue (never addressed by Apple), you should:

    In iOS <= 4 and iOS >= 6:

    UIViewController *vc = [[UIViewController alloc]init];
    [self presentModalViewController:vc animated:NO];
    [self dismissModalViewControllerAnimated:NO];
    [vc release];
    

    In iOS 5:

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window.subviews objectAtIndex:0];
    [view removeFromSuperview];
    [window addSubview:view];
    

    These will REALLY force UIKit to re-evaluate all your shouldAutorotate , supportedInterfaceOrientations, etc.

    0 讨论(0)
  • 2020-11-22 06:37

    For Monotouch you could do it this way:

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
        {
        return UIInterfaceOrientationMask.LandscapeRight;
    }
    
    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
        {
        return UIInterfaceOrientation.LandscapeRight;
    }
    
    0 讨论(0)
  • 2020-11-22 06:38

    I see the many answer but not get the particular idea and answer about the orientation but see the link good understand the orientation and remove the forcefully rotation for ios6.

    http://www.disalvotech.com/blog/app-development/iphone/ios-6-rotation-solution/

    I think it is help full.

    0 讨论(0)
  • 2020-11-22 06:41

    If you want all of our navigation controllers to respect the top view controller you can use a category so you don't have to go through and change a bunch of class names.

    @implementation UINavigationController (Rotation_IOS6)
    
    -(BOOL)shouldAutorotate
    {
        return [[self.viewControllers lastObject] shouldAutorotate];
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return [[self.viewControllers lastObject] supportedInterfaceOrientations];
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
    }
    
    @end
    

    As a few of the comments point to, this is a quick fix to the problem. A better solution is subclass UINavigationController and put these methods there. A subclass also helps for supporting 6 and 7.

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

    Just go to project.plist then add Supported interface orientation and then add only Portrait (bottom home button) and Portrait (top home button).

    You can add or remove there orientation as per your project requirement .

    Thanks

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

    The best way for iOS6 specifically is noted in "iOS6 By Tutorials" by the Ray Wenderlich team - http://www.raywenderlich.com/ and is better than subclassing UINavigationController for most cases.

    I'm using iOS6 with a storyboard that includes a UINavigationController set as the initial view controller.

    //AppDelegate.m - this method is not available pre-iOS6 unfortunately

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
    
    if(self.window.rootViewController){
        UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];
    }
    
    return orientations;
    }
    

    //MyViewController.m - return whatever orientations you want to support for each UIViewController

    - (NSUInteger)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskPortrait;
    }
    
    0 讨论(0)
提交回复
热议问题