How do I present a UIAlertController from the button that triggers it?

In iOS 26 there is a new way of displaying action sheets from the buttons that triggers them. I figured out that in SwiftUI you can just set the confirmationDialog view modifier on the button that triggers it.

However, I can't find a way to get this behavior in UIKit when a UIButton triggers an alert. Has anyone got this work?

The behavior is mentioned briefly in the "Get to know the new design system" session.

Answered by DTS Engineer in 847228022

For additional context:

@objc func showConfirmationDialog(_ sender: UIButton) {
    let alert = UIAlertController(
        title: "Are you sure?",
        message: "Do you want to proceed?",
        preferredStyle: .actionSheet
    )
    alert.addAction(UIAlertAction(title: "Cancel", style: .default,handler: nil))
    alert.addAction(UIAlertAction(title: "Confirm", style: .destructive, handler: { _ in
        
    }))
    alert.popoverPresentationController?.sourceItem = sender
    present(alert, animated: true, completion: nil)
}

Here when the sourceItem is set alert.popoverPresentationController?.sourceItem = sender the popover’s arrow points to the specified item

I think you only need to set the sourceView/sourceRect or barButtonItem properties for the popoverPresentationController of the UIAlertViewController before presenting it.

Until now this was only necessary for the actionSheet style on the iPad, but I think under iOS 26 this can be also used for the alert style to define the anchor point to which the UIAlertViewController is attached to.

You can set the sourceItem on the alert controller's popoverPresentationController, like this:

alertController.popoverPresentationController?.sourceItem = button


However, I think the effect only works if the button is a UIBarButtonItem. The same is the case in SwiftUI: toolbar bar buttons have the morph effect but regular buttons don't. Instead, the new behaviour in iOS 26 is to show the action sheet as a popover with an arrow, like on iPad. If no source is set, an alert is presented.

The barButtonItem property of UIPopoverPresentationController is deprecated. Use sourceItem which was added in iOS 16.

Accepted Answer

For additional context:

@objc func showConfirmationDialog(_ sender: UIButton) {
    let alert = UIAlertController(
        title: "Are you sure?",
        message: "Do you want to proceed?",
        preferredStyle: .actionSheet
    )
    alert.addAction(UIAlertAction(title: "Cancel", style: .default,handler: nil))
    alert.addAction(UIAlertAction(title: "Confirm", style: .destructive, handler: { _ in
        
    }))
    alert.popoverPresentationController?.sourceItem = sender
    present(alert, animated: true, completion: nil)
}

Here when the sourceItem is set alert.popoverPresentationController?.sourceItem = sender the popover’s arrow points to the specified item

How do I present a UIAlertController from the button that triggers it?
 
 
Q