ios - 如何在 Swift 中修复“UIAlertController 的 ActionSheet 具有冲突约束”错误
问题描述
我的代码在iOS12.1上运行成功,但是最近我将iOS、Xcode、macOS的版本更新到iOS12.2 Xcode10.2和macOS10.14.4时,我的项目出现了问题。我的应用程序界面没有问题,但是当我打开操作表时,它告诉我我的约束冲突。我该如何解决? 这是我的 UI 这是 Xcode 中的警告
我重置了界面的约束,但是不管怎么弄,还是一样的问题。
这些是我的代码,一旦我点击按钮,就会出现操作表,Xcode 告诉我我的约束是冲突的
@IBAction func newToDoBarButtonTapped(_ sender: UIBarButtonItem) {
let alertController = UIAlertController(title: "New or Edit", message: nil, preferredStyle: .actionSheet)
let newToDoAlertAction = UIAlertAction(title: "New Item", style: .default, handler: {(_) in
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let registerVC = mainStoryboard.instantiateViewController(withIdentifier: "NewToDoStoryboard") as! ToDoViewController
self.navigationController?.pushViewController(registerVC, animated: true)
})
let editInformationOfListAction = UIAlertAction(title: "Edit Information", style: .default, handler: {(_) in
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let registerVC = mainStoryboard.instantiateViewController(withIdentifier: "EditStoryboard") as! EditItemTableViewController
self.navigationController?.pushViewController(registerVC, animated: true)
})
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(newToDoAlertAction)
alertController.addAction(editInformationOfListAction)
alertController.addAction(cancelAction)
self.present(alertController,animated: true,completion: nil)
}
我除了没有关于冲突约束的警告。
解决方案
您可能必须手动使约束警报静音。
@IBAction func newToDoBarButtonTapped(_ sender: UIBarButtonItem) {
...
self.present(alertController,animated: true,completion: nil)
alertController.view.subviews.flatMap({$0.constraints}).filter{ (one: NSLayoutConstraint)-> (Bool) in
return (one.constant < 0) && (one.secondItem == nil) && (one.firstAttribute == .width)
}.first?.isActive = false
}