0

我有一个应用程序,其中一个标签栏控制器有 3 个标签,每个标签都有一个视图控制器。视图控制器 1 和 2 仅支持纵向模式,但选项卡 3 中的视图控制器支持所有方向 - 纵向、横向左侧、横向右侧和纵向倒置。对于选项卡 3,我们必须在设备处于纵向模式时显示特定视图,而在设备处于横向模式时显示另一个视图。

如果设备处于纵向模式并且用户单击选项卡 3,则视图在纵向模式下正确加载,然后如果我们将设备旋转到横向模式,横向视图将正确加载。

但是如果我们在选项卡 1 本身中将设备设置为横向模式,然后单击选项卡 3 ,则会出现问题,然后它显示屏幕以横向模式显示,但它显示为纵向视图。

当我试图通过 NSLogging 在委托方法中找出原因时

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

interfaceOrientation 的值为 1,即 UIInterfaceOrientationPortrait 并且控件进入为纵向模式编写的 if 条件

if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){

当我尝试在同一个委托方法中打印 UIDevice 方向的值时

UIDeviceOrientation interfaceOrientation = [UIDevice currentDevice].orientation;
NSLog(@" device orientation %u", interfaceOrientation);

控制台中打印的值是 4- 这是 UIDeviceOrientationLandscapeRight。

所以界面方向是 UIInterfaceOrientationPortrait 但设备方向是 UIDeviceOrientationLandscapeRight。

还有一件事,现在如果我将设备旋转到纵向模式,然后切换到横向模式,则会显示正确的横向视图,并且应用程序开始正常运行。

所以问题是,如果我在横向模式下加载选项卡 3,它不会正确加载,但是如果我们进入纵向模式,然后旋转到横向,如果从那里可以正常工作。

如果有人可以提供一些有用的建议,为什么这种行为会非常有益。

谢谢

4

2 回答 2

1

在 viewWillAppear 方法中将这些行添加到您的 Tab1 和 Tab2 中,

#import <objc/message.h>

- (void)viewWillAppear:(BOOL)animated {
    if(UIDeviceOrientationIsLandscape(self.interfaceOrientation)){
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait );
        }
    }
}
于 2013-09-20T09:22:46.690 回答
0

创建一个带有 UITabBarController 子类的 CustomTabBarController 类(例如:CustomTabBarController)

@interface CustomTabBarController : UITabBarController

将以下行添加到此类:

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

在您的应用程序委托或您正在初始化 UITabBarController 的任何地方,将这些实例替换为 CustomTabBarController 实例。

self.tabBarController = [[[CustomTabBarController alloc] init] autorelease];

将这些行添加到具有不同视图控制器支持的模型视图控制器中:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate {
    return NO;
}
于 2013-09-20T09:04:25.430 回答