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

Awe*_*ess 24 rotation orientation uiactionsheet ios ios8

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

我希望UIActionSheet遵循包含视图控制器的规则而不是旋转.思考?

我不希望这种情况发生: 截图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;
}
Run Code Online (Sandbox Code Playgroud)

Stu*_*low 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这失败了,肖像颠倒了. (2认同)

Bus*_*rod 11

我在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处阅读有关此代表的更多信息:

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


Awe*_*ess 5

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

"新的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];
Run Code Online (Sandbox Code Playgroud)

  • 当然,有一种新的"首选"方式,但为什么仅仅因为引入了新方法而改变/破坏旧的工作功能呢?这种新方式是获得原始行为的唯一方法吗? (5认同)
  • 在iOS7上,旧的UIActionSheet不会出现轮换问题.在iOS7及更早版本上使用UIActionSheet.在iOS8及更高版本上使用UIAlertController.检查NSClassFromString(@"UIAlertController")以确定要使用的代码. (2认同)