UIAlertController is Crashed (iPad)

后端 未结 10 1723
长情又很酷
长情又很酷 2020-12-13 17:58

I am using Xcode 6 to develop an iOS Application.

When I used UIAlertController, it can be worked well on iPhone 6 simulator, but crashes on iPad simula

相关标签:
10条回答
  • 2020-12-13 18:50

    To keep it device independent, rather use it as shown in the snippet below. This code will return nil for popoverPresentationController on an iPhone / compact size device, so you can safely use it in universal projects.

    if let popoverPresentationController = shareMenu.popoverPresentationController {
        popoverPresentationController.sourceView = self.view
        popoverPresentationController.sourceRect = sender.bounds
    }
    self.presentViewController(shareMenu, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-13 18:51

    I use this code, very easy and understand, maybe i can help for any

    //if iPhone
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            [self presentViewController:actionSheet animated:YES completion:nil];
        }
        //if iPad
        else {
            // Change Rect to position Popover
            UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:actionSheet];
            [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
    
    0 讨论(0)
  • 2020-12-13 18:52

    reference: ActionSheet Popover on iPad in Swift

    create popoverController :

    let alertController = UIAlertController(title: nil, message: "Alert message.", preferredStyle: .actionSheet)
    self.present(alertController, animated: true, completion: nil)
    

    if you want show alert with indicator view should:

    if let popoverController = alertController.popoverPresentationController {
        popoverController.barButtonItem = sender
    }
    

    with indicator

    show alert in center:

    if let popoverController = alertController.popoverPresentationController {
      popoverController.sourceView = self.view
      popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) 
    }
    

    center with indicator

    center no indicator:

    if let popoverController = alertController.popoverPresentationController {
      popoverController.sourceView = self.view
      popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
      popoverController.permittedArrowDirections = []
    }
    

    center no indicator

    0 讨论(0)
  • 2020-12-13 18:57

    I am also face same issue and finally got the solution. Here is my solution for Objective-C:

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Select you option:"
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@“share”
                                                         style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction *action) {
                                                           // do other things
                                                       }];
    [alertController addAction:action];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction *action) {
                                                         }];
    [alertController addAction:cancelAction];
    
    // Remove arrow from action sheet.
    [alertController.popoverPresentationController setPermittedArrowDirections:0];
    
    //For set action sheet to middle of view.
    CGRect rect = self.view.frame;
    rect.origin.x = self.view.frame.size.width / 20;
    rect.origin.y = self.view.frame.size.height / 20;
    alertController.popoverPresentationController.sourceView = self.view;
    alertController.popoverPresentationController.sourceRect = rect;
    
    [self presentViewController:alertController animated:YES completion:nil];
    
    0 讨论(0)
提交回复
热议问题