問題描述
我的應(yīng)用程序主要是縱向的,但是有一個(gè)視圖需要橫向.
My application is primarily portrait, however there is one view that REQUIRES a landscape orientation.
我的視圖包含在 UINavigationController 中,這(顯然)是導(dǎo)致此問題的原因.
My views are contained within a UINavigationController, which (apparently) is the cause of this issue.
除了一個(gè)之外的所有 UIViewControllers 都有這個(gè):
All UIViewControllers except one have this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
需要 Landscape 的 UIViewController 有這個(gè):
The UIViewController that requires Landscape has this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
現(xiàn)在,當(dāng)用戶到達(dá)橫向 UIViewController 時(shí),它會(huì)以縱向顯示.然后,用戶可以旋轉(zhuǎn)他們的手機(jī),它會(huì)按照我的意愿橫向顯示(鎖定為橫向).然后用戶繼續(xù)使用縱向 UIViewController,同樣的情況發(fā)生:它從橫向開始,然后他們旋轉(zhuǎn)手機(jī),它再次變成縱向(并鎖定為縱向).
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 之間似乎允許方向鎖定,但是自動(dòng)旋轉(zhuǎn)/以編程方式更改方向被某種方式阻止了.
It seems orientation locking is allowed between UIViewControllers, however auto-rotation / programmatically changing the orientation is somehow blocked.
如何強(qiáng)制手機(jī)更新到正確的方向?
有一個(gè)臨時(shí)解決方案:我可以檢測設(shè)備的方向并顯示一條消息,要求他們?cè)诓徽_時(shí)旋轉(zhuǎn)設(shè)備,但這不是最佳選擇.
推薦答案
我的一個(gè)應(yīng)用程序也有同樣的要求?。?!
I had the same requirement for one of my applications!!!
幸運(yùn)的是我找到了解決方案!
luckily I found a solution!
為了保持主視圖控制器的橫向,無論從哪個(gè)方向彈出/推送,我都做了以下事情:(在 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 上,先前的答案導(dǎo)致屏幕閃爍,而且 - 它不穩(wěn)定(在某些情況下它不再適合我.)所以,為了我的需要,我將代碼更改為:(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.
希望它會(huì)有所幫助!
這篇關(guān)于UINavigationController 強(qiáng)制旋轉(zhuǎn)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!