UIAlertController 的 actionSheet 在 iOS 12.2/12.3上给出了约束错误

在 iOS 12.2中,当使用 UIAlertControlleractionSheet时,Xcode 会出现约束错误?

同样的代码在 iOS 12.1上运行,没有错误。

我在 Xcode 10.2和10.1上测试了这段代码。

class ViewController: UIViewController {
    

let Click : UIButton = {
let button = UIButton(type: UIButton.ButtonType.system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("OK", for: .normal)
button.tintColor = UIColor.blue
button.addTarget(self, action: #selector(click(_:)), for: UIControl.Event.touchUpInside)
return button
}()


override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(Click)
Click.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
Click.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}


    

@objc func click(_ sender: UIButton) {
let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .actionSheet)
        

let deleteAction = UIAlertAction(title: "Delete", style: .default)
let saveAction = UIAlertAction(title: "Save", style: .default)
    

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        

optionMenu.addAction(deleteAction)
optionMenu.addAction(saveAction)
optionMenu.addAction(cancelAction)
        

self.present(optionMenu, animated: true, completion: nil)
}


}
[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:0x6000001b6ee0 UIView:0x7fe3b6513020.width == - 16   (active)>"
)


Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000001b6ee0 UIView:0x7fe3b6513020.width == - 16   (active)>

附注:

为了确保问题出现在 UIAlertController上,我删除了所有内容并更新了下面的代码,但是我收到了同样的错误:

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
    

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
        

let optionMenu = UIAlertController(title: "Test", message: "Choose Option", preferredStyle: .actionSheet)
        

let deleteAction = UIAlertAction(title: "Delete", style: .default)
let saveAction = UIAlertAction(title: "Save", style: .default)
        

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        

optionMenu.addAction(deleteAction)
optionMenu.addAction(saveAction)
optionMenu.addAction(cancelAction)
        

self.present(optionMenu, animated: true, completion: nil)
}
    

}
12740 次浏览

这是 iOS 版本中的一个 bug:

  • 12.2
  • 12.3
  • 12.4
  • 13.0
  • 13.1
  • 13.2
  • 13.2.3
  • 13.3
  • 13.4
  • 13.4.1
  • 13.5
  • 13.5.1
  • 13.6
  • 14.0
  • 14.1
  • 14.2
  • 14.4

我们唯一能做的就是向苹果公司提交一份 bug 报告(我已经这么做了,你也应该这么做)。

如果问题仍然存在,我将尝试更新新版本 iOS 的答案。感谢帮助。