iOS 7界面方向

6

我有一个应用程序,总是以纵向模式显示。

但在某些地方,我必须支持横向的媒体库。

项目默认支持的方向为纵向。因此,媒体库只能以纵向模式显示。

如果我将项目设置更改为同时支持纵向和横向,则媒体库可以正常工作,但我不能控制其他视图控制器仅以纵向模式显示。

我尝试了几种方法,如shouldAutoRotate,但没有一种方法起作用。

有什么解决方法吗?

谢谢

编辑:

问题已解决 :)

首先,我配置了项目以支持所有方向。然后我添加了这个方法到AppDelegate.m中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}

接下来我所做的是在每个视图控制器中禁止方向,除了我想要横向和纵向方向的那个。

禁止方向的代码(iOS 7):

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskPortrait);
}

感谢所有回答我的人 :)

请查看我的帖子:http://stackoverflow.com/questions/19422373/how-to-set-one-of-the-screens-in-landscape-mode-in-iphone/19423135#19423135 - Divya Bhaloidiya
在我的情况下,您的代码无法正常工作。即使我在“支持的界面方向”和“首选界面方向”中指定了纵向模式,视图也会旋转到横向位置。如果我将shouldAutorotate设置为true或false也没有任何变化。 - Antonio Sesto
我也遇到了同样的问题。iOS 7及以上版本使得处理方向变得非常复杂。 - Fmessina
2个回答

7
在我的 iPhone 应用程序中,仅支持纵向视图,但根据要求,需要仅支持横向视图的一个视图,在这种情况下,我使用以下方式并且它对我有所帮助:
在您的应用程序委托.h文件中:
@interface PlayWithWSWithLibAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

       BOOL flagOrientationAll;
}

@property (assign) BOOL flagOrientationAll;

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

在您想要在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;
}

查看此帖子也可以:如何将 iPhone 中的一个屏幕设置为横向模式?


请记得在应用委托的 .m 文件中添加 @synthesize flagOrientationAll;。 - AlvaroSantisteban

1

您需要在同一视图控制器中创建另一个类,用于呈现您的媒体。

在这个类中,您可以指定它支持的方向,仅当您呈现媒体时才支持横向方向。

我给您举个例子,我的应用程序只支持横向模式,但是当我使用图像选择器并且它只支持纵向模式时,我会为该视图更改方向。

#pragma mark - Image PICKER

@interface NonRotatingUIImagePickerController : UIImagePickerController

@end

@implementation NonRotatingUIImagePickerController



- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait ;
}

@end

当我使用ImagePicker时,我使用了我自己创建的类的对象。

所以我定义如下。

UIImagePickerController* picker = [[NonRotatingUIImagePickerController alloc] init];

对于图片选择器,它只在竖屏模式下显示。
如果您希望在特定视图中仅使用横屏方向,请将"portrait"更改为"landscape"。
希望这能帮到您。

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