首页 文章

iOS 8 UIActionSheet忽略视图控制器supportedInterfaceOrientations和shouldAutorotate

提问于
浏览
23

我有一个通过plist文件配置的应用程序,以支持纵向,横向左和横向右方向(即UISupportedInterfaceOrientations设置为UIInterfaceOrientationPortrait,UIInterfaceOrientationLandscapeLeft和UIInterfaceOrientationLandscapeRight) . 但是我将支持的方向限制在视图控制器内部 . 如果我在视图控制器的视图上显示UIActionSheet然后旋转设备,则UIActionSheet将旋转到新方向 . 这仅发生在运行iOS 8(GM Seed)的设备上 .

我希望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 回答

  • 0

    以下是基于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;
    }
    
  • 5

    我在iOS 8中遇到了类似的问题,如果你现在想要坚持使用UIAlertView和UIActionSheet,而不是迁移到UIAlertController,我找到了一个可能有用的解决方法 .

    我的应用程序允许横向和纵向,但仅限于选择视图 . 大多数时候它只允许肖像 . 我希望UIAlertView和UIActionSheet只显示Portrait(因为它们所在的视图只允许使用Portrait) .

    不幸的是,使用iOS 8时,警报和操作表现在会旋转,忽略窗口根视图控制器的shouldAutorotate方法 . 即使警报旋转,警报下方的视图和状态栏也会在纵向中保持可见状态 . 如果警报采取输入,键盘也保持肖像,所以它真的没有吸引力 .

    我即将放弃,但最终找到了有效的东西,即使它不理想 . 将其放入您的app委托中,并根据需要进行调整 . 我期待更好的解决方案(可能只是使用新类) . 这里的字符串比较显然很蹩脚,这将覆盖您在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:

  • 11

    获悉UIAlertController取代了iOS 8中的UIActionSheet .

    “新的UIAlertController类将UIActionSheet和UIAlertView类替换为在应用程序中显示警报的首选方式 . ”

    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];
    
  • 0

    添加到@ user3750435的答案

    由于OP已经在 Info.plist 中定义了他/她想要的旋转蒙版,我们不需要在这里重新定义蒙版 . UIApplication可以通过supportedInterfaceOrientationsForWindow:将这些面具返回给我们 . 因此,该方法可以更简单,如下所示:

    -(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];
    }
    
  • 21
    • (NSUInteger)应用程序:(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;
    

    }

    谢谢这对我有用

相关问题