Fixed orientation for Iphone in universal app

非 Y 不嫁゛ 提交于 2019-12-13 01:45:06

问题


I have developed a universal app I want to support all orientation for IPAD but for Iphone I just want UIInterfaceOrientationPortrait and UIInterfaceOrientationPortraitUpsideDown only for this I have

-(BOOL)shouldAutorotate {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        return NO;
    }else{
        return YES;
    }
}
- (NSUInteger)supportedInterfaceOrientations{

     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
         return UIInterfaceOrientationMaskPortrait;
     }else{
        return UIInterfaceOrientationMaskAll;
     }

} 
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){

    return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ;
     }else{
         return YES;
     }

}

But still is not stop the orientation in landscape


回答1:


Simply disable all other orientations except the UIInterfaceOrientationPortrait and UIInterfaceOrientationPortraitUpsideDown one for the iPhone from the Summary like in the description below. In the pic the Upside down is disabled please enable it. Thanks.




回答2:


For portrait upside down you have to use navigation controller

1) I created a new subclass of UINavigationController, and added shouldAutorotate and supportedInterfaceOrientation methods:

     // MyNavigationController.h:
     #import <UIKit/UIKit.h>

     @interface MyNavigationController : UINavigationController

     @end

    // MyNavigationController.m:
     #import "MyNavigationController.h"
     @implementation MyNavigationController
     ...
    - (BOOL)shouldAutorotate {
    return YES;
     }

     - (NSUInteger)supportedInterfaceOrientations {
     return UIInterfaceOrientationMaskAll;
     }
    ...
       @end

2) In the AppDelegate, I did use my new subclass to show my root ViewController (it is introScreenViewController, a UIViewController subclass) and did set the self.window.rootViewController, so it looks that:

      nvc = [[MyNavigationController alloc]              initWithRootViewController:introScreenViewController];
     nvc.navigationBarHidden = YES;
      self.window.rootViewController = nvc;
      [window addSubview:nvc.view];
   [window makeKeyAndVisible];


来源:https://stackoverflow.com/questions/17421300/fixed-orientation-for-iphone-in-universal-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!