呈现 UIAlertController(以操作表样式)导致神秘的自动布局警告
Presenting UIAlertController (in actionsheet style) caused mysterious autolayout warning
可以使用如下简单项目重现此问题:
当用户单击导航栏上的 +
按钮时,代码实例化并呈现一个 UIAlertController
in actionsheet 样式。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func buttonTapped(_ sender: Any) {
let actionSheet = UIAlertController(title: nil,
message: nil,
preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Create Income",
style: .default) { _ in
print("Income!") })
actionSheet.addAction(UIAlertAction(title: "Create Outcome",
style: .default) { _ in
print("Outcome!")})
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in })
self.present(actionSheet, animated: true, completion: nil)
}
}
UI 正确显示。问题是 Xcode 在操作 sheet 出现时发出以下自动布局警告:
2020-03-12 11:24:57.002113+0800 alertcontroler_test[12757:936231] [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.
(
"<NSLayoutConstraint:0x600002d11b30 UIView:0x7f851961c560.width == - 16 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600002d11b30 UIView:0x7f851961c560.width == - 16 (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.
当动作 sheet 出现时,我检查 Xcode 中的 UI 层次结构,发现一些子视图后跟一个感叹号:
我 google 关于这个,但没有发现有人报告类似的问题(即,当 UIAlertController
出现时自动布局警告)。我找到了一些关于如何在 UIViewAlertForUnsatisfiableConstraints()
上设置断点以及如何调试 assemble 代码的信息。但我认为这是 UIAlertController
的一个内部问题,我创建和呈现 UIAlertController
的方式是常见且典型的。有什么建议我应该怎么做?
这是在 Xcode (11.3.1) 和 iOS (13.3) 的最新版本中找到的。如果我将样式更改为警报,问题就消失了。任何帮助将不胜感激。
这是一个 Apple 团队未修复的错误,此错误与动画相关的警报和操作表有关。您可以点击这些链接来验证这一点:-
UIAlertController's actionSheet gives constraint error on iOS 12.2 / 12.3
解决方案是将动画值作为 false 传递,例如:-
controller.present(alertController, animated: false, completion: nil)
我展示了没有动画的 UIAlertController,并且警告消失了。
或者您可以尝试在 Whosebug 的一个链接中提到的这个解决方案:-
@IBAction func buttonTapped(_ sender: UIButton) {
...
self.present(alertController,animated: true,completion: nil)
alertController.view.subviews.flatMap({[=11=].constraints}).filter{ (one: NSLayoutConstraint)-> (Bool) in
return (one.constant < 0) && (one.secondItem == nil) && (one.firstAttribute == .width)
}.first?.isActive = false
}
可以使用如下简单项目重现此问题:
当用户单击导航栏上的 +
按钮时,代码实例化并呈现一个 UIAlertController
in actionsheet 样式。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func buttonTapped(_ sender: Any) {
let actionSheet = UIAlertController(title: nil,
message: nil,
preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Create Income",
style: .default) { _ in
print("Income!") })
actionSheet.addAction(UIAlertAction(title: "Create Outcome",
style: .default) { _ in
print("Outcome!")})
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in })
self.present(actionSheet, animated: true, completion: nil)
}
}
UI 正确显示。问题是 Xcode 在操作 sheet 出现时发出以下自动布局警告:
2020-03-12 11:24:57.002113+0800 alertcontroler_test[12757:936231] [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.
(
"<NSLayoutConstraint:0x600002d11b30 UIView:0x7f851961c560.width == - 16 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600002d11b30 UIView:0x7f851961c560.width == - 16 (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.
当动作 sheet 出现时,我检查 Xcode 中的 UI 层次结构,发现一些子视图后跟一个感叹号:
我 google 关于这个,但没有发现有人报告类似的问题(即,当 UIAlertController
出现时自动布局警告)。我找到了一些关于如何在 UIViewAlertForUnsatisfiableConstraints()
上设置断点以及如何调试 assemble 代码的信息。但我认为这是 UIAlertController
的一个内部问题,我创建和呈现 UIAlertController
的方式是常见且典型的。有什么建议我应该怎么做?
这是在 Xcode (11.3.1) 和 iOS (13.3) 的最新版本中找到的。如果我将样式更改为警报,问题就消失了。任何帮助将不胜感激。
这是一个 Apple 团队未修复的错误,此错误与动画相关的警报和操作表有关。您可以点击这些链接来验证这一点:-
UIAlertController's actionSheet gives constraint error on iOS 12.2 / 12.3
解决方案是将动画值作为 false 传递,例如:-
controller.present(alertController, animated: false, completion: nil)
我展示了没有动画的 UIAlertController,并且警告消失了。
或者您可以尝试在 Whosebug 的一个链接中提到的这个解决方案:-
@IBAction func buttonTapped(_ sender: UIButton) {
...
self.present(alertController,animated: true,completion: nil)
alertController.view.subviews.flatMap({[=11=].constraints}).filter{ (one: NSLayoutConstraint)-> (Bool) in
return (one.constant < 0) && (one.secondItem == nil) && (one.firstAttribute == .width)
}.first?.isActive = false
}