問題描述
我的應用程序主要是縱向的,但是有一個視圖需要橫向.
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).
UIViewController 之間似乎允許方向鎖定,但是自動旋轉/以編程方式更改方向被某種方式阻止了.
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];
P.S.在 sdk 3.2.5 ios 5.0.1 上測試.
P.S.Tested on sdk 3.2.5 ios 5.0.1.
附:在 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 強制旋轉的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!