macOS 上的 iOS 应用程序不显示 UIAlertController
我有一个 iOS 应用程序,在 macOS 上运行时会显示警报以响应用户操作。
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)
这会因错误而崩溃:
您的应用程序已呈现来自ExampleViewController (
) 的UIAlertControllerStyleActionSheet 样式的UIAlertController ( )。具有此样式的 UIAlertController 的 modalPresentationStyle 是 UIModalPresentationPopover。您必须通过警报控制器的 popoverPresentationController 提供此弹出窗口的位置信息。您必须提供 sourceView 和 sourceRect 或 barButtonItem。如果在呈现警报控制器时不知道此信息,您可以在 UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation 中提供它。
在 iOS 或 iPadOS 上运行时,不需要在 popoverPresentationController
上设置属性,因为它使用 actionSheet
样式。此视图控制器无法访问 UIBarButtonItem
,因此无法设置 barButtonItem
属性。如果我设置了 sourceView
和 sourceRect
属性(如下所示),它会记录自动布局错误并且不显示警报:
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
在 macOS 上运行的 iOS 应用程序中?请注意:这不是 Catalyst 应用程序,这是在 Apple Silicon Mac 上本地运行的 iOS 应用程序。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论