如何强制竖屏显示?

5

如果我的应用程序最初支持两种方向,如何在 iOS 7 中强制使用纵向方向?


1
不太清楚,您能再解释一下吗?您是希望整个应用程序只支持竖屏模式吗? - Gajendra Rawat
1
可能是从横向视图控制器推送时强制使用纵向方向的重复问题。 - Tomer Even
4个回答

6

要对整个应用程序进行设置,请打开项目文件,转到“常规”选项卡,更改设置:

enter image description here

或直接在 Info.plist 文件中更改:

enter image description here

如果您只想在特定的视图控制器上进行设置,请重写 supportedInterfaceOrientations

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

您可以在官方的UIViewController文档中了解更多有关第二种方法的内容。也许,您会发现更适合您特定问题的方法。


简单而正确,但要注意-(BOOL)shouldAutorotate方法。 它不应该被覆盖,或者返回YES,否则它将无法工作。 - Moose

2
UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:c animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];

这段代码可能不符合苹果的标准,不确定...但是它是一个好的黑客技巧。 - Divya Bhaloidiya
当我们使用这段代码时,我们的应用程序将无法获得苹果公司的生产批准。 - Prabhu Natarajan
1
你能解释一下为什么这个有效吗?另外,那些方法在iOS 6中已被弃用。可能苹果公司会在iOS 8中决定将其删除。苹果公司淘汰一个方法后不久就移除它并非第一次。 - rtiago42
@PrabhuNatarajan,你怎么能说它会被苹果拒绝呢?你在哪里找到这样的声明? - Pawan Rai
1
不支持iOS 8。 - Tomer Even
iOS 8 中出现了闪烁效果。 - Mihir Oza

0
请使用以下方法: 在您的应用程序委托.h文件中。
@interface PlayWithWSWithLibAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

       BOOL flagOrientationAll;
}

@property (assign) BOOL flagOrientationAll;

在您的应用程序委托.m文件中添加以下方法。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    //NSLog(@"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
    if(flagOrientationAll == YES){
        return UIInterfaceOrientationMaskPortrait;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

在您想要在 iPhone 设备的纵向和横向旋转的视图中实现以下方式。
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.delegate = self;

    PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *) [[UIApplication sharedApplication] delegate];
    delegate.flagOrientationAll = YES;
 }
}

-(void)viewWillDisappear:(BOOL)animated
{
    //NSLog(@"viewWillDisappear -- Start");
     PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *)[[UIApplication sharedApplication] delegate];
        delegate.flagOrientationAll = NO;
}

这是我的线程,与你的问题有些相似:iOS 7界面方向


-3
#import <objc/message.h>

objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeRight);//here you may change your desired Orientation

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接