【问题标题】:UIAlertController Unable to satisfy constraintsUIAlertController 无法满足约束
【发布时间】:2015-10-30 13:37:47
【问题描述】:

我正在用UIAlertController 转换我所有的UIActionSheetUIAlertView

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

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)]>

谢谢大家。

【问题讨论】:

  • 该错误提示您尝试将alert.view.translatesAutoresizingMaskIntoConstraints 设置为NO。你试过这个吗?
  • 是的,我试过[alert.view setTranslatesAutoresizingMaskIntoConstraints:NO];,同样的问题
  • 你有没有想过这个问题?我也有同样的问题。

标签: ios objective-c iphone uiactionsheet uialertcontroller


【解决方案1】:

我在提交UIAlertController 样式的 ActionSheet 时也遇到了类似的问题。

另一个可能的原因是错误地设置了UIPopoverPresentationControllerpermittedArrowDirections。就我而言,弹出框的 sourceView 位于顶部,但我已将 permittedArrowDirections 设置为 UIPopoverArrowDirectionDown,这会导致“无法同时满足约束”错误。设置为UIPopoverArrowDirectionUp后错误消失了。

【讨论】:

  • 补充:通常最好允许箭头的任何方向,这样控制器只会显示适合的方向:popPresenter.permittedArrowDirections = UIPopoverArrowDirection.any
【解决方案2】:

我遇到了同样的问题,因为我最初不明白sourceViewsourceRect 必须引用与操作表相关的按钮/视图。这样弹出框就会出现在它旁边。如果改为使用整个视图,则弹出框将出现在屏幕之外,从而产生约束错误。

【讨论】:

    【解决方案3】:

    我收到此错误是因为我专注于 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 焦点有关。

    【讨论】:

    • 您的代码可以通过编写if let textField = subview as? UITextField 来改进,而不是检查类型然后强制转换。你的卷发风格也是错误的。
    【解决方案4】:

    我自己找到了解决方案:

    这来自以下几行:

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

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

    【讨论】:

    • 好吧,我这样做是为了替换我的应用程序的所有 alertView 和 actionSheet(大约 200-300),并且效果很好
    • 从 iOS 9 开始,这对我不起作用。它崩溃并说您需要定义 BOTH。
    • 其实两者都应该设置,但应该指的是弹出框应该出现在旁边的视图/按钮。
    • 您需要从 iOS 10 和 11 开始设置。对于 iPad。然而,这个答案似乎与最初提出的问题无关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2014-10-27
    • 2014-06-19
    • 1970-01-01
    相关资源
    最近更新 更多