UIAlertController 无法满足约束

UIAlertController Unable to satisfy constraints

我正在将所有 UIActionSheetUIAlertView 转换为 UIAlertController

我的问题是当我尝试呈现 UIAlertController 样式的 ActionSheet 时。 这是我的代码:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title"
                                                               message:@"My message"
                                                        preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancelAction];

UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:OKAction];

UIAlertAction *destroyAction = [UIAlertAction actionWithTitle:@"Destroy" style:UIAlertActionStyleDestructive handler:nil];
[alert addAction:destroyAction];

UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
popPresenter.sourceView = self.view;
popPresenter.sourceRect = self.view.bounds;

[self presentViewController:alert animated:YES completion:nil];

这是错误:

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) 
(
    "<NSLayoutConstraint:0x7c00ab40 H:[UIView:0x7c0764c0(304)]>",
    "<NSLayoutConstraint:0x7b6c7f80 _UIAlertControllerView:0x7b6c2e50'title'.width >= UIView:0x7b6c3230.width>",
    "<NSLayoutConstraint:0x7ccdfe40 _UIAlertControllerView:0x7b6c2e50'title'.width == UIView:0x7b6efbe0.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x7c33b9d0 h=--& v=--& H:[UIView:0x7c1094e0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7c00ab40 H:[UIView:0x7c0764c0(304)]>

谢谢大家

我自己找到了解决方案:

这来自以下几行:

popPresenter.sourceView = self.view;
popPresenter.sourceRect = self.view.bounds;

我必须设置 sourceViewsourceRect 但不能同时设置。

我也遇到了类似的问题,目前是UIAlertController风格的ActionSheet

另一个可能的原因是 UIPopoverPresentationControllerpermittedArrowDirections 设置错误。在我的例子中,弹出窗口的 sourceView 位于顶部,但我已将 permittedArrowDirections 设置为 UIPopoverArrowDirectionDown,这会导致 "Unable to simultaneously satisfy constraints" 错误。我设置为UIPopoverArrowDirectionUp.

后错误消失了

我遇到了同样的问题,因为我最初不明白 sourceViewsourceRect 必须指的是与动作相关的 button/view [=] 15=]。这样弹出窗口就会出现在它旁边。如果改为使用整个视图,则弹出窗口将出现在屏幕之外,从而产生约束错误。

我收到此错误是因为我专注于 UITextField。首先 UITextField 似乎有点问题,所以 the code below from this SO post 让我解决了这个问题,

extension UIView
{
    func fixInputAssistant()
    {
        for subview in self.subviews
        {
            if type(of: subview) is UITextField.Type
            {
                let item = (subview as! UITextField).inputAssistantItem
                item.leadingBarButtonGroups = []
                item.trailingBarButtonGroups = []
            }
            else if subview.subviews.count > 0
            {
                subview.fixInputAssistant()
            }
        }
    }
}

但是在验证我的 UITextField 时,我的 UIAlertController 会引发您提到的约束冲突。为了解决这个问题,我需要在显示 UIAlertController 之前失去焦点,因此

firstName.resignFirstResponder()
surname.resignFirstResponder()
emailAddress.resignFirstResponder()
... etc...

请注意确定这是否与您的特定场景相关,但请注意这可能与 UITextField 焦点有关。

我最近自己遇到了这个问题,虽然这个问题的现有答案确实有帮助,但我发现一些额外的信息很有用:

  • 当给定的参数会导致警报在屏幕外绘制时,似乎会发生此错误
  • 您必须提供 barButtonItem 或同时提供 sourceViewsourceRect。如果您只提供 sourceViewsourceRect(不是两者),您的应用程序将会崩溃(至少对我来说是这样)。
  • 当提供sourceViewsourceRect时,sourceRect相对sourceView,而不是视图控制器的来源显示警报
  • sourceViewsourceRect 可以是任何东西,不必像 Fil 的回答所指示的那样与按下的按钮相关

Chubao 的回答是将允许的箭头方向设置为任意确实有助于解决这个问题,因为只要给定的 sourceRect 在屏幕上,UIKit 就会选择允许警报在屏幕上的最佳箭头方向以及。但是,如果您提供了您认为有效的箭头方向并且 运行 进入此错误,您似乎更可能犯了错误并提供了 sourceView and/or sourceRect 你不打算这样做,导致警报出现意外位置,可能值得确保你传递了正确的 sourceViewsourceRect.