首页 文章

使用iOS 8在iPad上正确呈现UIAlertController

提问于
浏览
162

在iOS 8.0中,Apple引入了UIAlertController来取代UIActionSheet . 不幸的是,Apple没有添加任何有关如何呈现它的信息 . 我在hayaGeek 's blog, however, it doesn'上找到了关于它的entry似乎可以在iPad上运行 . 视图完全错位:

错位:
Misplaced image

正确:
enter image description here

我使用以下代码在界面上显示它:

let alert = UIAlertController()
    // setting buttons
    self.presentModalViewController(alert, animated: true)

还有其他方法可以为iPad添加它吗?或者苹果只是忘了iPad,还是没有实现?

6 回答

  • 1

    在Swift 2中,你想做这样的事情来在iPhone和iPad上正确显示它:

    func confirmAndDelete(sender: AnyObject) {
        guard let button = sender as? UIView else {
            return
        }
    
        let alert = UIAlertController(title: NSLocalizedString("Delete Contact?", comment: ""), message: NSLocalizedString("This action will delete all downloaded audio files.", comment: ""), preferredStyle: .ActionSheet)
        alert.modalPresentationStyle = .Popover
    
        let action = UIAlertAction(title: NSLocalizedString("Delete", comment: ""), style: .Destructive) { action in
            EarPlaySDK.deleteAllResources()
        }
        let cancel = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .Cancel) { action in
    
        }
        alert.addAction(cancel)
        alert.addAction(action)
    
        if let presenter = alert.popoverPresentationController {
            presenter.sourceView = button
            presenter.sourceRect = button.bounds
        }
        presentViewController(alert, animated: true, completion: nil)
    }
    

    如果您未设置演示者,则最终会在 -[UIPopoverPresentationController presentationTransitionWillBegin] 上的iPad上出现例外情况,并显示以下消息:

    致命异常:NSGenericException您的应用程序提供了样式UIAlertControllerStyleActionSheet的UIAlertController(<UIAlertController:0x17858a00>) . 具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover . 您必须通过警报控制器的popoverPresentationController为此弹出窗口提供位置信息 . 您必须提供sourceView和sourceRect或barButtonItem . 如果在显示警报控制器时未知此信息,则可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供该信息 .

  • 72

    Update for Swift 3.0 and higher

    let actionSheetController: UIAlertController = UIAlertController(title: "SomeTitle", message: nil, preferredStyle: .actionSheet)
    
        let editAction: UIAlertAction = UIAlertAction(title: "Edit Details", style: .default) { action -> Void in
    
            print("Edit Details")
        }
    
        let deleteAction: UIAlertAction = UIAlertAction(title: "Delete Item", style: .default) { action -> Void in
    
            print("Delete Item")
        }
    
        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in }
    
        actionSheetController.addAction(editAction)
        actionSheetController.addAction(deleteAction)
        actionSheetController.addAction(cancelAction)
    
    //        present(actionSheetController, animated: true, completion: nil)   // doesn't work for iPad
    
        actionSheetController.popoverPresentationController?.sourceView = yourSourceViewName // works for both iPhone & iPad
    
        present(actionSheetController, animated: true) {
            print("option menu presented")
        }
    
  • 96

    在iPad上,警报将使用新的_289656显示为弹出窗口,它要求您使用sourceView和sourceRect或barButtonItem为弹出窗口的表示指定锚点

    • barButtonItem

    • sourceView

    • sourceRect

    为了指定锚点,您需要获取对UIAlertController的UIPopoverPresentationController的引用,并设置其中一个属性,如下所示:

    alertController.popoverPresentationController.barButtonItem = button;
    

    示例代码:

    UIAlertAction *actionDelete = nil;
    UIAlertAction *actionCancel = nil;
    
    // create action sheet
    UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:actionTitle message:nil
                                          preferredStyle:UIAlertControllerStyleActionSheet];
    
    // Delete Button
    actionDelete = [UIAlertAction
                    actionWithTitle:NSLocalizedString(@"IDS_LABEL_DELETE", nil)
                    style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    
                        // Delete
                        // [self deleteFileAtCurrentIndexPath];
                    }];
    
    // Cancel Button
    actionCancel = [UIAlertAction
                    actionWithTitle:NSLocalizedString(@"IDS_LABEL_CANCEL", nil)
                    style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                        // cancel
                        // Cancel code
                    }];
    
    // Add Cancel action
    [alertController addAction:actionCancel];
    [alertController addAction:actionDelete];
    
    // show action sheet
    alertController.popoverPresentationController.barButtonItem = button;
    alertController.popoverPresentationController.sourceView = self.view;
    
    [self presentViewController:alertController animated:YES
                     completion:nil];
    
  • 7

    您可以使用 UIPopoverPresentationController 从弹出框中显示 UIAlertController .

    在Obj-C中:

    UIViewController *self; // code assumes you're in a view controller
    UIButton *button; // the button you want to show the popup sheet from
    
    UIAlertController *alertController;
    UIAlertAction *destroyAction;
    UIAlertAction *otherAction;
    
    alertController = [UIAlertController alertControllerWithTitle:nil
                                                          message:nil
                               preferredStyle:UIAlertControllerStyleActionSheet];
    destroyAction = [UIAlertAction actionWithTitle:@"Remove All Data"
                                             style:UIAlertActionStyleDestructive
                                           handler:^(UIAlertAction *action) {
                                               // do destructive stuff here
                                           }];
    otherAction = [UIAlertAction actionWithTitle:@"Blah"
                                           style:UIAlertActionStyleDefault
                                         handler:^(UIAlertAction *action) {
                                             // do something here
                                         }];
    // note: you can control the order buttons are shown, unlike UIActionSheet
    [alertController addAction:destroyAction];
    [alertController addAction:otherAction];
    [alertController setModalPresentationStyle:UIModalPresentationPopover];
    
    UIPopoverPresentationController *popPresenter = [alertController 
                                                  popoverPresentationController];
    popPresenter.sourceView = button;
    popPresenter.sourceRect = button.bounds;
    [self presentViewController:alertController animated:YES completion:nil];
    
  • 240

    这是一个快速的解决方案:

    NSString *text = self.contentTextView.text;
    NSArray *items = @[text];
    
    UIActivityViewController *activity = [[UIActivityViewController alloc]
                                          initWithActivityItems:items
                                          applicationActivities:nil];
    
    activity.excludedActivityTypes = @[UIActivityTypePostToWeibo];
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        //activity.popoverPresentationController.sourceView = shareButtonBarItem;
    
        activity.popoverPresentationController.barButtonItem = shareButtonBarItem;
    
        [self presentViewController:activity animated:YES completion:nil];
    
    }
    [self presentViewController:activity animated:YES completion:nil];
    
  • 18

    2018年更新

    我刚刚因为这个原因拒绝了应用程序,并且很快就解决了从使用操作表到警报的过程 .

    工作了一个魅力,并通过App Store测试员就好了 .

    对于每个人来说可能不是一个合适的答案,但我希望这能帮助你们中的一些人迅速摆脱困境 .

相关问题