Ipad UIAlertAction does not darken the surrounding color

I used the UIAlertController to display the popup.


On the iPhone, the pop-up is well visible and the surrounding color is dark.


However, the iPad has an error.


let alertVC = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)


if UIDevice.current.userInterfaceIdiom == .pad

{

alertVC.popoverPresentationController?.sourceView = self

alertVC.popoverPresentationController?.sourceRect = CGRect(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.maxY, width: 0, height: 0)

alertVC.popoverPresentationController?.permittedArrowDirections = []

}

// add alertVC.addAction()


DispatchQueue.main.async

{

guard let parentVC = self.parentViewController as? MoreViewController else { return }

parentVC.present(alertVC, animated: true, completion: nil)

}


I solved the error that occurred in the iPad through search.


However, when the Alert is displayed on the iPad, the surroundings do not become dark.


Please help me.

AFAIK that's not an error but intended behavior.


In fact,

- on iPhone, you get a cancel button ; background is darkened to show it is inactive, because alert is modal

- on iPad, no cancel button ; you cancel by tapping on surrounding, which is active (non modal). That's why is does not darken.


Personal note: I usually prefer to set style to .alert for iPhone


        var alertStyle = UIAlertController.Style.alert = alertStyle = .alert
        if UIDevice.current.userInterfaceIdiom == .pad {
            alertStyle = UIAlertController.Style.actionSheet
        }
    let alertVC = UIAlertController(title: nil, message: nil, preferredStyle: alertStyle
    if UIDevice.current.userInterfaceIdiom == .pad {          
        alertVC.popoverPresentationController?.sourceView = self
        alertVC.popoverPresentationController?.sourceRect = CGRect(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.maxY, width: 0, height: 0)
        alertVC.popoverPresentationController?.permittedArrowDirections = []
    }
Ipad UIAlertAction does not darken the surrounding color
 
 
Q