0

I know how to make an action sheet pop over a button on iPad but not for iPhone. it seems that on iphone the action sheet doesn't have popoverPresentationController:

    @IBAction func addChannel(_ sender: Any) {
        let sender = sender as? UIBarButtonItem
        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        let addChanel = UIAlertAction(title: "Add a Channel", style: .default) { (_) in
            self.addChannel()
        }
        let addContact = UIAlertAction(title: "Add a Contact", style: .default){ _ in
            
            self.addContact()
        }
        actionSheet.addAction(addChanel)
        actionSheet.addAction(addContact)
        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel))
//        actionSheet.popoverPresentationControlle
        if let popover = actionSheet.popoverPresentationController{
            actionSheet.popoverPresentationController?.barButtonItem = sender
            actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down
        }
        
        self.present(actionSheet, animated: true)
        

    }

I want the action sheet pop over a button just like what WeChat + button does. Other stackoverflow answers are too old and not workable Any idea is welcome!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Stan
  • 602
  • 6
  • 23

1 Answers1

2

Source.

You cannot use UIAlertController as a popover on iPhones. One of alternatives is using a UITableView or something inside new UIViewController, which you can create like described below (Swift 5.0).

import UIKit

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
@IBOutlet private weak var button: UIButton!

@IBAction func buttonAction() {
    let vc = UIViewController()
    vc.view.backgroundColor = UIColor.blue
    vc.preferredContentSize = CGSize(width: 200, height: 200)
    vc.modalPresentationStyle = .popover
    let ppc = vc.popoverPresentationController
    ppc?.permittedArrowDirections = .any
    ppc?.delegate = self
    ppc?.sourceView = button
    present(vc, animated: true, completion: nil)
}

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}
}

Also, popoverPresentationController has property called barButtonItem which you can assign to your nav bar button.

enter image description here

enter image description here

ChooGoo
  • 85
  • 8
  • This methods adds a new viewcontoller over the viewcontroller and what I want is just a UIAlertController poping over the viewcontroller – Stan Nov 10 '20 at 06:09
  • You cannot do this on iPhones, UIAlertController is not meant to be a popover on these devices. One of alternatives is using a UITableView or something inside new UIViewController. – ChooGoo Nov 10 '20 at 07:19
  • 1
    Ok, can you make this point clear in your answer so that more people can know this and I will accept your answer then – Stan Nov 10 '20 at 07:33