UINavigationController强制旋转 [英] UINavigationController Force Rotate
问题描述
我的应用程序主要是人像,但是有一个视图需要横向方向。
My application is primarily portrait, however there is one view that REQUIRES a landscape orientation.
我的视图包含在UINavigationController中,显然这是原因这个问题。
My views are contained within a UINavigationController, which (apparently) is the cause of this issue.
除了一个之外的所有UIViewControllers都有:
All UIViewControllers except one have this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
需要Landscape的UIViewController具有:
The UIViewController that requires Landscape has this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
现在,当用户到达格局UIViewController时会发生什么在肖像。然后,用户可以旋转他们的手机,并按照我想要的方式显示在风景中(锁定到横向)。然后用户继续前进到肖像UIViewController,同样的事情发生:它从横向开始,然后他们旋转手机,它再次成为肖像(并锁定到肖像)。
Now, what happens is when the user reaches the landscape UIViewController, it is shown in portrait. The user can then rotate their phone and it displays in landscape as I want it to (locking to landscape). The user then progresses onwards to a portrait UIViewController and the same happens: it start in landscape, then they rotate their phone and it becomes portrait again (and locks to portrait).
似乎UIViewControllers之间允许方向锁定,但是自动旋转/以编程方式更改方向会以某种方式被阻止。
It seems orientation locking is allowed between UIViewControllers, however auto-rotation / programmatically changing the orientation is somehow blocked.
如何强制手机更新为正确的方向?
有一个临时解决方案:我可以检测设备的方向并显示一条消息,要求他们旋转设备如果它不正确,但这不是最佳的。
推荐答案
我对我的一个要求相同申请!!!
I had the same requirement for one of my applications!!!
幸运的是我找到了解决方案!
luckily I found a solution!
为了保持主视图控制器的格局,无论从哪个方向弹出/推动,我都做了以下事情:(在viewWillAppear中:)
//set statusbar to the desired rotation position
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
//present/dismiss viewcontroller in order to activate rotating.
UIViewController *mVC = [[[UIViewController alloc] init] autorelease];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
在sdk上安装3.2.5 ios 5.0.1。
P.S.Tested on sdk 3.2.5 ios 5.0.1.
PS在iOS 8之前的回答导致一些屏幕闪烁,而且 - 它不稳定(在某些情况下它不再适用于我。)因此,根据我的需要,我将代码更改为:(ARC)
P.S. On iOS 8 previous answer results some screen flickering and also - it is not stable (In some cases It does not work for me anymore.) So, for my needs, I changed the code to: (ARC)
//set statusbar to the desired rotation position
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
[self.navigationController presentViewController:[UIViewController new] animated:NO completion:^{
dispatch_after(0, dispatch_get_main_queue(), ^{
[self.navigationController dismissViewControllerAnimated:NO completion:nil];
});
}];
//I added this code in viewDidDissapear on viewController class, which will be popped back.
希望它会有所帮助!
这篇关于UINavigationController强制旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!