Swift UIAlertController -> ActionSheet iPad iOS8 Crashes

前端 未结 8 1435
北恋
北恋 2020-12-02 12:20

currently i\'m running into big trouble with my ActionSheet. On iPhone it works great, but on iPad it only crashes

I create a new project with only one button

<
相关标签:
8条回答
  • 2020-12-02 12:26

    Nate Cook is totally right however I would do it so I detect if it is iPad or iPhone.

    This is for barButtonItem:

    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad ){
    
                if let currentPopoverpresentioncontroller = alertController.popoverPresentationController{
                    currentPopoverpresentioncontroller.barButtonItem = sender as! UIBarButtonItem
                    currentPopoverpresentioncontroller.permittedArrowDirections = UIPopoverArrowDirection.down;
                    self.present(alertController, animated: true, completion: nil)
                }
            }else{
                self.present(alertController, animated: true, completion: nil)
            }
    
    0 讨论(0)
  • 2020-12-02 12:37

    If you want to present it in the centre with no arrows on iPads [Swift 3+]:

        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 = []
        }
        self.present(alertController, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-02 12:39

    If you want show your alert in center and no arrow, I tryed this, it works on iOS 11.2

    swift 4.2 version:

    let actionSheet = UIAlertController ......
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad ){
    
            if let currentPopoverpresentioncontroller = actionSheet.popoverPresentationController{
                currentPopoverpresentioncontroller.permittedArrowDirections = []
                currentPopoverpresentioncontroller.sourceRect = CGRect(x: (self.view.bounds.midX), y: (self.view.bounds.midY), width: 0, height: 0)
                currentPopoverpresentioncontroller.sourceView = self.view
                self.present(actionSheet, animated: true, completion: nil)
            }
        }else{
            self.present(actionSheet, animated: true, completion: nil)
        }
    

    Objective C version:

    UIAlertController* actionSheet 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        NSArray *empty;
        UIPopoverPresentationController *currentPopOverPresentationController = [actionSheet popoverPresentationController];
        currentPopOverPresentationController.permittedArrowDirections = empty;
        currentPopOverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds), 0, 0);
        currentPopOverPresentationController.sourceView = self.view;
    
        [self presentViewController:actionSheet animated:YES completion:nil];
    }else{
        [self presentViewController:actionSheet animated:YES completion:nil];
    }
    
    0 讨论(0)
  • 2020-12-02 12:40

    To follow up on Nate Cook's answer. If your button is a UIBarButtonItem it may be necessary to perform casting on sender.

    if let popoverController = alertController.popoverPresentationController {
        popoverController.barButtonItem = sender as! UIBarButtonItem
    }
    self.presentViewController(alertController, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-02 12:41

    The error message is telling you that you need to give the alert controller's popoverPresentationController a location so that it can position itself properly. This is easy to do -- just check to see if there's a popover controller and add the sender as the source.

    If your button is a UIBarButtonItem:

    if let popoverController = alertController.popoverPresentationController {
        popoverController.barButtonItem = sender
    }
    self.presentViewController(alertController, animated: true, completion: nil)
    

    Otherwise:

    if let popoverController = alertController.popoverPresentationController {
        popoverController.sourceView = sender
        popoverController.sourceRect = sender.bounds
    }
    self.presentViewController(alertController, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-02 12:46

    try this

    alertController.popoverPresentationController?.sourceView = self.view
    
    0 讨论(0)
提交回复
热议问题