2015-10-30 3 views
3

Я конвертирую все свои UIActionSheet и UIAlertView с UIAlertController.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)]> 

Спасибо всем.

+0

Ошибка указывает на то, что вы пытаетесь настроить 'alert.view.translatesAutoresizingMaskIntoConstraints' на' NO'. Вы пробовали это? – n00neimp0rtant

+0

Да, я пробовал '[alert.view setTranslatesAutoresizingMaskIntoConstraints: NO];', same issue –

+1

Вы когда-нибудь это понимали? У меня та же проблема. – Fil

ответ

-2

Я нашел решение сам:

это происходит из линий:

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

Я должен установить sourceView ИЛИ sourceRect но не оба.

+0

Ну, я сделал это, чтобы заменить все alertView & actionSheet моего приложения (около 200-300), и он отлично работает –

+0

Это не работает для меня, как и для iOS 9. Он падает и говорит, что вам нужно определить BOTH. – Mike

+1

Фактически оба должны быть установлены, но следует обратиться к кнопке view /, рядом с которой должен появиться popover. – Fil

2

Я также сталкиваюсь с аналогичной проблемой в настоящем UIAlertController стиля ActionSheet.

Еще одна возможная причина этого - установить permittedArrowDirectionsUIPopoverPresentationController ошибочно. В моем случае sourceView popover находится вверху, но я установил permittedArrowDirections в UIPopoverArrowDirectionDown, что приводит к ошибке «Невозможно одновременно удовлетворить ограничения». Ошибка исчезла после того, как я установил ее в UIPopoverArrowDirectionUp.

0

У меня была такая же проблема, потому что я изначально не понял, что sourceView и sourceRect должны обратиться к кнопке/представлению, которое было нажато в связи с листом действия. Таким образом, рядом с ним появляется popover. Если вместо этого используется весь вид, то popover появится за пределами экрана, что создает ошибки ограничений.

Смежные вопросы