UIAlertController和UIViewAlertForUnsatisfiableConstraints错误

22

昨天我升级到Xcode 10.2并开始使用Swift 5,在调用UIAlertController照片提示时发现了这个错误。我记得在Xcode 10.1中没有看到过它。

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

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.

我阅读了这个问题如何在UIViewAlertForUnsatisfiableConstraints上设置陷阱?并且能够将错误定位到我的UIAlertController(用红色突出显示)

enter image description here

这是我的代码:


 @objc private func promptPhoto() {

    let prompt = UIAlertController(title: "Choose a Photo",
                                   message: "",
                                   preferredStyle: .actionSheet)

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self

    let camerAction = UIAlertAction(title: "Camera", style: .default) { _ in
      guard self.isCameraAccessible() else {
        self.showAlert(title: "Oops", message: "Camera is not available")
        return
      }
      imagePicker.sourceType = .camera
      imagePicker.allowsEditing = true
      self.present(imagePicker, animated: true)
    }

    let libraryAction = UIAlertAction(title: "Photo Library", style: .default) { _ in
      imagePicker.sourceType = .photoLibrary
      imagePicker.allowsEditing = true
      self.present(imagePicker, animated: true)
    }

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

    prompt.addAction(camerAction)
    prompt.addAction(libraryAction)
    prompt.addAction(cancelAction)

    present(prompt, animated: true) {
      // Prevent closing the prompt by touch up outside the prompt.
      prompt.view.superview?.subviews[0].isUserInteractionEnabled = false
    }

  }

我已经尝试过在我的promptPhoto()方法中使用以下代码设置UIAlertController的宽度,但没有成功。

let width: NSLayoutConstraint = NSLayoutConstraint(item: prompt.view!,
                                                   attribute: NSLayoutConstraint.Attribute.width,
                                                   relatedBy: NSLayoutConstraint.Relation.equal,
                                                   toItem: nil,
                                                   attribute: NSLayoutConstraint.Attribute.notAnAttribute,
                                                   multiplier: 1,
                                                   constant: self.view.frame.width) 

prompt.view.addConstraint(width)


有没有一种方法可以控制UIAlertController的宽度,以便我可以摆脱错误消息?谢谢。

1
在你的 Build Setting -> Swift Compiler - Language 中使用 Swift 5 还是 Swift 4.2 - Nizzam
@Nizzam,Swift语言版本已设置为Swift 5。 - bvh
我会说不用担心,这似乎是UIAlertController内部布局不一致的问题。 - Tamás Sengel
12
这似乎是一个错误:http://openradar.appspot.com/49289931 - lenooh
还没有修复吗?一直打印错误。 - erotsppa
有人找到了上述问题的解决方案吗? - Nidhi Tiwari
1个回答

2

这可能是Xcode的一个bug。苹果公司仍未修复此问题。此错误发生在最新版本的Xcode 11.3.1中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接