首页 文章

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

提问于
浏览
1

我已经开发了一个通用应用程序,我想支持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 回答

  • 6

    只需在 Summary 中禁用除 UIInterfaceOrientationPortraitUIInterfaceOrientationPortraitUpsideDown 之外的所有其他方向,如下面的描述中所示 . 在图片中,颠倒被禁用请启用它 . 谢谢 .

    enter image description here

  • 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];
    

相关问题