[英]Allow One View to Support Multiple Orientations While Others Do Not iPhone
我遇到了通過UITabBarController
控制多個視圖的情況。 這些視圖之一是通過UINavigationController
控制的。 該應用程序應僅對所有視圖都支持縱向...除了一個單獨的視圖之外。 該視圖被推到導航控制器的堆棧上,並且應該允許縱向和橫向。
我已經嘗試過所有可以想到的解決方案,但是,要么解決方案不起作用,要么更糟糕的是完全無法預測。
有人以干凈的方式解決了這個問題嗎?
使用presentModalViewController
或pushViewController
呈現的視圖控制器應使用以下方法支持任何方向:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
使用這種方法,當您以橫向顯示控制器時,它也將正確以橫向顯示。
我有同樣的問題。 您必須為UITabBarController
實現shouldAutorotateToInterfaceOrientation:
UITabBarController
並為要在所有視圖控制器中支持的所有方向返回YES。 並且對於所有其他視圖控制器,使用相同的方法僅對要在每個視圖控制器中支持的方向返回YES。
要實現shouldAutorotateToInterfaceOrientation:
對於UITabBarController,可以將UITabBarController子類化,但是一種更簡單的方法是在UITabBarController上實現類別,然后僅實現該方法:
@implementation UITabBarController(orientation)
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
{
return YES; // or whatever you need
}
@end
注意:輸入網絡瀏覽器; 沒有編譯。 :-)
您可以在最后將它放在您的應用程序委托.m文件中。
我發現UITabBarController僅對肖像返回YES,這顯然也會影響所有從屬視圖控制器。 以我為例,我有一個下級視圖控制器,我想同時支持縱向和橫向,這解決了這一問題。
如果導航欄位於選項卡欄的內部,則唯一的選擇是將特定視圖顯示為模式視圖(選項卡欄要求所有視圖都支持自動旋轉的方向)。 所以基本上需要景觀的單視圖控制器應該實現
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
// Create the landscape version of the view and present it modally
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
然后,通過檢測肖像方向並將其用於父級,以模態視圖控制器的相同方法關閉模態視圖[self.parentViewController dismissModalViewControllerAnimated: animated]
對於iOS-6,我已經做到了,它運行得很好
(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
這將為您提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.