【发布时间】:2014-07-13 11:35:25
【问题描述】:
我有一个应用程序可以在方向更改时更改视图:[[UIApplication sharedApplication] statusBarOrientation]。在 iPhone 上,它可以工作,但在 iPad 上,它给我横向而不是纵向和纵向而不是横向。
- 当我在 UIDeviceOrientationDidChangeNotification 之外调用 getRelevantFrame 方法时,它将返回正确的帧。
- 响应通知时会返回相反的帧(如果是横向,则返回纵向,反之亦然)。
- 两个版本(iPod/iPhone + iPad)使用相同的代码,但这只会在 iPad 版本上中断
这是我用来计算相关帧的代码:
编辑:使用 rob5408 的建议。改为使用 UI_USER_INTERFACE_IDIOM()。
+ (CGRect)getRelevantFrame {
//Get orientation
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { //iPhone/iPod
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return [self frameiPhoneLandscape];
}
else {
return [self frameiPhonePortrait];
}
} else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { //iPad
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"iPad Landscape");
return [self frameiPadLandscape];
}
else {
NSLog(@"iPad Portrait");
return [self frameiPadPortrait];
}
}
return CGRectMake(0, 0, 0, 0);
}
这是我使用的通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
这是我用来读取方向变化的代码:
- (void) didRotate:(NSNotification *)notification
{
NSLog(@"Parent: %@", self.parentController);
if(self.parentController) {
[self setFrame:[TASFrames getRelevantFrame]];
}
}
【问题讨论】:
-
做一个坚实的自己并使用
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad而不是使用视图边界。这可能无法解决问题,但它可以为您解决未来的问题。 -
感谢您的建议。从现在开始,我一定会使用它。
标签: ios objective-c ipad ios7 rotation