iphone - UITabBar 识别纵向或横向方向

标签 iphone objective-c ios xcode uiinterfaceorientation

虽然在大多数情况下,方向对我的应用程序来说工作正常,但我在 iPad 1 上测试时遇到问题。如果我的设备倾斜角度相对较低,则有时会在选项卡中导航标签栏以横向模式显示,但页面调用纵向模式 uiview,然后尝试以横向模式呈现它,搞砸了我的 UI。

我想弄清楚是否有一种方法可以锁定“如果标签栏出现在横向模式下,总是调用横向 UIViews,如果在纵向模式下,总是调用纵向 UIView。”

在每个 View Controller 上,我都设置了以下内容:

- (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);

}
}

我还使用以下认为可以解决问题的方法调整了应用程序委托(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;

}

最佳答案

我认为您不想测试 [[UIDevice currentDevice] orientation]。正如文档所指出的那样,值可能与应用程序 UI 的实际方向不同。我认为您需要依赖对 [UIViewController shouldRotateTo...] 和 [UIViewController willRotateTo...] 的调用。

关于iphone - UITabBar 识别纵向或横向方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10585832/

相关文章:

iphone - UITabBar 隐藏时留下一个白色矩形

ios - 即使应用旋转后,AVMutableComposition 也无法旋转视频

ios - 如何为不同的 xAxis 标签呈现自定义标签颜色?

iphone - 由于 Alamofire 的符号链接(symbolic link)错误,xcodebuild 无法创建存档

ios - 想要在我的应用程序中访问 iPhone 常规设置内容

ios - FSSDKShareKit - FBSDKShareDialog 在 init iOS 上崩溃

ios - UIPageControl自定义

ios - 用于生产应用程序的 Crashlytics CLS_Log 与 NSLog

ios - 在prepareForSegue方法中UIView为nil

ios - UIWebView 和 WKWebView 的键盘 "done"按钮按下事件