【问题标题】:How to do "actionSheet showFromRect" in iOS 8?如何在 iOS 8 中执行“actionSheet showFromRect”?
【发布时间】:2014-08-07 03:34:55
【问题描述】:

在 iOS 7 中,我通过 "showFromRect" 显示 actionSheet:

[actionSheet showFromRect:rect inView:view animated:YES];

但在 iOS 8 中,这不起作用。他们替换了实现并建议我们使用 UIAlertController。那么如何像弹出框一样显示这个actionSheet呢?

【问题讨论】:

  • 我试过使用 UIPopoverController,里面有 UIAlertController,还是不行。 UIPopoverController *popOverController = [[UIPopoverController alloc] initWithContentViewController:alertController];

标签: uipopovercontroller ios8 uiactionsheet actioncontroller


【解决方案1】:

使用 UIAlertController 您可以访问 popoverPresentationController 属性来设置 sourceView(又名 inView)和 sourceRect(又名 fromRect)。这给出了与之前的 showFromRect:inView 相同的外观:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];

// Set the sourceView.
alert.popoverPresentationController.sourceView = self.mySubView;

// Set the sourceRect.
alert.popoverPresentationController.sourceRect = CGRectMake(50, 50, 10, 10);

// Create and add an Action.
UIAlertAction *anAction = [UIAlertAction actionWithTitle:@"Action Title" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"Action Pressed");
}];

[alert addAction:anAction];

// Show the Alert.
[self presentViewController:alert animated:YES completion:nil];

【讨论】:

    【解决方案2】:

    我和你一样也遇到这个问题,但是我的情况是我在iPad设备上的UIWindow.view中显示了UIActionSheet,而UIWindow没有设置rootViewController

    所以,我发现,如果我们在rootViewController 等于nil 的窗口中显示UIActionSheetUIActionSheet 就无法显示出来。

    我的解决办法是设置

    window.rootViewController = [[UIViewController alloc] init];
    

    那么,

    [actionSheet showFromRect:rect inView:window.rootViewController.view animated:YES].
    

    希望对您有所帮助!

    【讨论】:

      【解决方案3】:

      根据这个https://developer.apple.com/library/ios/documentation/Uikit/reference/UIActionSheet_Class/index.html线程 UIActionSheet 已弃用,您应该使用 UIAlertController 而不是 UIActionSheet。

      谢谢。

      【讨论】:

        【解决方案4】:

        我发现重点是在 iOS 7 中,您在使用 "showFromRect: inView:" 时实际上在新窗口中显示 actionSheet ;

        但在 iOS 8 中,您仅在您发送参数的视图上显示 actionSheet

        所以解决办法是发送

        self.superView
            [actionSheet showFromRect:rect inView:[self.superView] animated:YES];
        

        我和我的同事随机尝试弄明白了。

        【讨论】:

        • 不幸的是它在弹出窗口中不起作用:Presenting view controllers on detached view controllers is discouraged <UIViewController: 0x7a67fba0>.
        猜你喜欢
        • 2014-11-10
        • 1970-01-01
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 2014-09-15
        • 2016-12-15
        • 2010-11-30
        • 1970-01-01
        相关资源
        最近更新 更多