我目前在iOS 5上遇到屏幕旋转问题。在iOS 6上,一切正常,但即使调用了通知UIDeviceOrientationDidChangeNotification
,旋转也无法在iOS 5上运行。
我正在使用Storyboard
,这就是我ViewController
的样子:
为了旋转屏幕,我使用以下方法:
CustomTabBarViewController.mm(UITabBarController
)
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
if (isLandscapeOK) {
// for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (isLandscapeOK) {
// for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
return (interfaceOrientation == UIInterfaceOrientationMaskAllButUpsideDown);
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
TabBarItemController.mm(UINavigationView
)
-(BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
LoyaltyListController.mm(UIViewController
)
- (void) didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)
{
self.navigationController.navigationBarHidden = YES;
[self hideTabBar:self.tabBarController];
portraitView.hidden = YES;
landscapeView.hidden = NO;
}
else if (orientation == UIDeviceOrientationPortrait)
{
self.navigationController.navigationBarHidden = NO;
[self showTabBar:self.tabBarController];
portraitView.hidden = NO;
landscapeView.hidden = YES;
[notificationView setFrame:CGRectMake(0, - (notificationView.frame.size.height + 40), notificationView.frame.size.width, notificationView.frame.size.height)];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
[(CustomTabBarViewController*)[self tabBarController] setLandscapeOK:YES];
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationMaskAllButUpsideDown);
}
以下是iOS 5的屏幕:
人像:
风景:(没有旋转)
这是它应该是什么样子:
有什么想法吗?
答案 0 :(得分:1)
对于iOS 5,请使用- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
在iOS 6中,已弃用,但如果您使用它则无关紧要。 它只会在iOS 5.0或更低版本的情况下被调用。
<强>更新强>: UIInterfaceOrientationMaskAllButUpsideDown 不是UIInterfaceOrientation枚举类型。 在iOS 6中使用遮罩来提供遮罩值,以便在iOS 5中提供您想要支持的方向组合。
您必须检查interfaceOrientaion是否为
UIInterfaceOrientationLandscapeLeft,UIInterfaceOrientationLandscapeRight, UIInterfaceOrientationPortrait or UIInterfaceOrientationPortraightUpsideDown.
并根据您要支持的所有方向返回YES或NO。
在您的情况下使用:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
/* If you are supporting all, simply return YES,
if you aren't supporting any return NO, otherwise Use || and check which ones you wanna return YES for.
In this case you wanna support all except UpsideDown hence use this. */
}
答案 1 :(得分:-1)
你必须返回YES以支持iOS 5及之前的所有方向及其在iOS中的弃用而不是在iOS 6中调用,你已经正确实现了iOS6方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; }