2014-09-13 39 views
23

我有經由plist文件配置成支持肖像的應用程序,景觀左和右橫向取向(即UISupportedInterfaceOrientations設置爲UIInterfaceOrientationPortrait,UIInterfaceOrientationLandscapeLeft,和UIInterfaceOrientationLandscapeRight)。但是我已經將支持的方向限制在視圖控制器內部。如果我在視圖控制器的視圖上顯示UIActionSheet,然後旋轉設備,UIActionSheet旋轉到新的方向。這隻發生在運行iOS 8(GM種子)的設備上。iOS 8的UIActionSheet忽略視圖控制器supportedInterfaceOrientations和shouldAutorotate

我想UIActionSheet遵循包含視圖控制器的規則,而不是旋轉。思考?

我不希望這樣的事情發生: screenshot http://oi58.tinypic.com/20tgto2.jpg

UIViewController的示例代碼:

- (IBAction)onTouch:(id)sender 
{ 
    UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"hi" 
                 delegate:nil 
               cancelButtonTitle:@"Cancel" 
             destructiveButtonTitle:nil 
               otherButtonTitles:@"Open", nil]; 

    [actionSheet showInView:self.view]; 
} 

#pragma mark - Rotation methods 

- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

回答

5

獲悉,UIAlertController iOS中取代UIActionSheet 8

「新UIAlertController類取代UIActionSheet和U​​IAlertView類作爲在您的應用程序中顯示警報的首選方式。「

https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"hi" 
                      message:nil 
                    preferredStyle:UIAlertControllerStyleActionSheet]; 

[alertController addAction:[UIAlertAction actionWithTitle:@"Open" 
                style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction *action) { 
                // open something 
               }]]; 

[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" 
                style:UIAlertActionStyleCancel 
               handler:nil]]; 

[self presentViewController:alertController animated:YES completion:nil]; 
+5

當然,有一種新的「首選」方式,但爲什麼只是因爲引入了一種新方法而改變/破壞舊的工作功能呢?這種新方式是獲得原始行爲的唯一方法嗎? – Bushrod 2014-09-18 19:04:59

+0

不,因爲iOS7用戶仍然需要處理舊的警報/工作表。顯然是一個需要修復的錯誤。 – Morrowless 2014-09-25 04:27:29

+2

在iOS7上,舊的UIActionSheet不會出現旋轉問題。在iOS7及更舊版本上使用UIActionSheet。在iOS8和更新版本上使用UIAlertController。檢查NSClassFromString(@「UIAlertController」)以確定使用哪個代碼。 – Awesomeness 2014-10-05 14:13:12

11

我有一個類似的問題與iOS 8,並找到一個解決辦法,如果你想與UIAlertView中和UIActionSheet堅持現在可以幫助,而不是遷移到UIAlertController。

我的應用程序允許橫向和縱向,但只在選定的視圖。大多數時候它只允許肖像。我想讓UIAlertView和UIActionSheet只顯示肖像(因爲它們所在的視圖只允許肖像)。

不幸的是,在iOS 8中,警報和操作表現在旋轉,忽略窗口根視圖控制器的shouldAutorotate方法。即使警報旋轉,警報下方的視圖和狀態欄仍可在肖像中顯示。如果警報需要輸入,鍵盤也會保持縱向,所以它確實沒有吸引力。

我即將放棄,但終於找到了一些可行的東西,即使它不理想。將其放入您的應用程序委託中,並根據需要進行調整。我期待着更好的解決方案(可能只是使用新的類)。這裏的字符串比較顯然是蹩腳的,並且這將覆蓋Info.plist中設置爲支持的方向的任何內容。至少這是值得去......

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    NSString *viewControllerClassName = [NSString stringWithUTF8String:object_getClassName(window.rootViewController)]; 
    if ([viewControllerClassName isEqualToString:@"_UIAlertShimPresentingViewController"]) { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
    else { 
     return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; 
    } 
} 

瞭解更多關於此委託在這裏:

https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:supportedInterfaceOrientationsForWindow:

+0

這是否會導致App Store因爲使用私有類而被拒絕? – sethfri 2014-10-06 19:04:30

+0

如果您擔心「_UI ...」,請嘗試下面的user3750435的變體。 「使用」類名來確定何時不旋轉並不是最安全的事情,但不應該是違規行爲,因爲它不會調用該類的任何方法/屬性。作爲另一種選擇,您可以將窗口與您自己已知的窗口進行匹配。我的用途是企業,所以沒有被拒絕的風險。 – Bushrod 2014-10-07 20:01:29

21

這裏是基於Busrod解決了一些修改我的解決方法,因爲我有用他的解決方法在UIActionSheet一些問題:

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    UIViewController *presentedViewController = window.rootViewController.presentedViewController; 
    if (presentedViewController) { 
     if ([presentedViewController isKindOfClass:[UIActivityViewController class]] || [presentedViewController isKindOfClass:[UIAlertController class]]) { 
      return UIInterfaceOrientationMaskPortrait; 
     } 
    } 
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; 
} 
+0

是的,這會起作用。謝謝! 我選擇使用新的UIAlertController(如果可用)。塊的使用相當不錯,我很快就會放棄iOS 7的支持。 – Awesomeness 2014-10-05 14:16:22

+2

這將失敗,縱向顛倒。 – Vignesh 2014-12-02 06:47:06

+0

如果您想要顛倒支持,請改爲返回UIInterfaceOrientationMaskAll。 – 2014-12-10 21:11:55

0

添加到@ user3750435的回答

由於OP已定義了他/她想要的Info.plist中的旋轉蒙版,因此我們無需在此重新定義蒙版。UIApplication可以通過 supportedInterfaceOrientationsForWindow:返回給我們那些面具。 (UIApplication的*)應用supportedInterfaceOrientationsForWindow::(一個UIWindow *)窗口 {

UIViewController *presentedViewController = window.rootViewController.presentedViewController; 
if (presentedViewController) { 
    if ([presentedViewController isKindOfClass:[UIActivityViewController class]] || [presentedViewController isKindOfClass:[UIAlertController class]]) { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; 

}

- (NSUInteger)應用

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    UIViewController *presentedViewController = window.rootViewController.presentedViewController; 
    if (presentedViewController) { 
     if ([presentedViewController isKindOfClass:[UIActivityViewController class]] || [presentedViewController isKindOfClass:[UIAlertController class]]) { 
      return UIInterfaceOrientationMaskPortrait; 
     } 
    } 
    return [application supportedInterfaceOrientationsForWindow:window]; 
} 
0

:那麼,該方法可以這樣更簡單謝謝這對我有用

相關問題