UIAlertController无法满足约束

时间:2015-10-30 13:37:47

标签: ios objective-c iphone uiactionsheet uialertcontroller

我正在使用UIActionSheet转换所有UIAlertViewUIAlertController

我的问题是当我尝试呈现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)]>

谢谢大家。

4 个答案:

答案 0 :(得分:2)

我目前在样式ActionSheet的push中也遇到了类似的问题。

另一个可能的原因是错误地设置了UIAlertController permittedArrowDirections。在我的例子中,popover的sourceView位于顶部,但我已将UIPopoverPresentationController设置为permittedArrowDirections,这导致&#34;无法同时满足约束&#34;错误。将其设置为UIPopoverArrowDirectionDown后错误消失。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:-3)

我自己找到了解决方案:

这来自以下几行:

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

我必须设置sourceViewsourceRect,但不能同时设置。{/ p>