UITabBar识别纵向或横向

时间:2012-05-14 14:57:53

标签: iphone objective-c ios xcode uiinterfaceorientation

虽然在大多数情况下,方向适用于我的应用程序,但我在iPad上测试时遇到问题1.如果我将设备倾斜到相对较低的角度,有时会浏览标签标签栏以横向模式显示,但页面调用纵向模式uiview,然后尝试以横向模式呈现它,搞砸了我的UI。

我正在试图找出是否有一种锁定方法“如果标签栏以横向模式显示,则始终调用横向UIViews,如果处于纵向模式,则始终调用肖像UIView。”

在每个视图控制器上,我设置了以下内容:

- (void)viewDidLoad
{
[super viewDidLoad];

// iPad-specific condition here
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){
        self.view = self.portraitViewiPad;
    }
    else {
        self.view = self.landscapeViewiPad;
    }
}
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

// iPad-specific condition here
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {

        //show portrait XIB here
        self.view = self.portraitViewiPad;

    } else {

        //show landscape XIB here
        self.view = self.landscapeViewiPad;

    }
}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
    // iPad-specific interface here
    return YES;
}
else
{
    // For iPhone and iPod touch interface
    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}
}

我还使用下面的方法调整了app delegate,以便解决问题:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//CALLS RELOAD METHODS HERE AND EACH UIVIEW IS PROPERLY BEING CALLED
}

更新

通过检查状态栏的方向并相应地显示正确的uiview来纠正此问题。以下是我更新viewDidLoad方法的方法:

if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationLandscapeLeft){

    NSLog(@"Left landscape detected");

    self.view = self.landscapeViewiPad;


} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationLandscapeRight){

    NSLog(@"Right landscape detected");

    self.view = self.landscapeViewiPad;

} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortrait){

    NSLog(@"Portrait orientation detected");

    self.view = self.portraitViewiPad;

} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortraitUpsideDown){

    NSLog(@"Upsidedown Portrait detected");

    self.view = self.portraitViewiPad;

}

1 个答案:

答案 0 :(得分:2)

我认为你不想做测试[[UIDevice currentDevice] orientation]。正如文档所述,值可能与应用UI的实际方向不同。我想你会依赖于对[UIViewController shouldRotateTo ...]和[UIViewController willRotateTo ...]的调用。