虽然在大多数情况下,我的应用程序的方向都可以正常工作,但我在 iPad 1 上测试时遇到问题。如果我将设备倾斜到相对较低的角度,则有时在浏览选项卡的选项卡时bar 以横向模式出现,但页面调用纵向模式 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);
}
}
我还使用以下方法调整了应用程序委托,认为可以解决该问题:
- (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;
}