在通用应用程序中固定Iphone的方向

时间:2013-07-02 08:53:10

标签: iphone orientation

我开发了一个通用应用程序我想支持IPAD的所有方向但是对于Iphone我只想要UIInterfaceOrientationPortrait和UIInterfaceOrientationPortraitUpsideDown只为此我有

-(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;
     }

}

但仍然没有停止景观方向

2 个答案:

答案 0 :(得分:6)

只需在摘要中禁用 iPhone UIInterfaceOrientationPortraitUIInterfaceOrientationPortraitUpsideDown除外的所有其他方向,如下面的说明所示。 在图片中,已禁用“颠倒”,请启用。感谢。

enter image description here

答案 1 :(得分:1)

对于纵向倒置,您必须使用导航控制器

1)我创建了一个新的UINavigationController子类,并添加了shouldAutorotate和supportedInterfaceOrientation方法:

     // 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)在AppDelegate中,我确实使用了我的新子类来显示我的根ViewController(它是introScreenViewController,一个UIViewController子类)并设置了self.window.rootViewController,所以看起来:

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