I have an iOS app that when run on macOS will present an alert in response to a user action.
let alertController = UIAlertController(title: "Choose an action", message: nil, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in
// ...
})
alertController.addAction(UIAlertAction(title: "Action 1", style: .default, handler: { _ in
// ...
}))
alertController.addAction(UIAlertAction(title: "Action 2", style: .default, handler: { _ in
// ...
}))
present(alertController, animated: true, completion: nil)
This crashes with the error:
Your application has presented a UIAlertController (<UIAlertController: 0x135010400>) of style UIAlertControllerStyleActionSheet from ExampleViewController (<ExampleViewController: 0x133826c00>). The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.
When running on iOS or iPadOS setting the properties on the popoverPresentationController
is not required because this is using the actionSheet
style. This view controller does not have access to a UIBarButtonItem
so setting the barButtonItem
property isn't an option. If I do set the sourceView
and sourceRect
properties (as shown below) it instead logs autolayout errors and no alert is shown:
if #available(iOS 14.0, *), ProcessInfo.processInfo.isiOSAppOnMac {
alertController.popoverPresentationController?.sourceView = view
alertController.popoverPresentationController?.sourceRect = view.bounds
}
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600003a1a3a0 h=--& v=--& UIView:0x12914af30.height == 13 (active)>",
"<NSLayoutConstraint:0x600003a1f750 UIView:0x129067d90.height >= 44 (active)>",
"<NSLayoutConstraint:0x600003a1fed0 _UIAlertControllerView:0x1290677a0'Choose an action'.height == UIView:0x129067d90.height (active)>",
"<NSLayoutConstraint:0x600003a19040 UIView:0x12914af30.height == _UIAlertControllerView:0x1290677a0'Choose an action'.height (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003a1f750 UIView:0x129067d90.height >= 44 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600003a1a350 h=--& v=--& UIView:0x12914af30.width == 0 (active)>",
"<NSLayoutConstraint:0x600003a4e0d0 UIView:0x129067d90.width == 270 (active)>",
"<NSLayoutConstraint:0x600003a1fe80 _UIAlertControllerView:0x1290677a0'Choose an action'.width >= UIView:0x129067d90.width (active)>",
"<NSLayoutConstraint:0x600003a18ff0 UIView:0x12914af30.width == _UIAlertControllerView:0x1290677a0'Choose an action'.width (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003a4e0d0 UIView:0x129067d90.width == 270 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
How can I use UIAlertController
in an iOS app running on macOS? Please note: this not a Catalyst app, this is an iOS app running natively on an Apple Silicon Mac.