2015-10-30 129 views
3

我正在將所有UIActionSheetUIAlertViewUIAlertController轉換。UIAlertController無法滿足約束條件

我的問題是當我嘗試呈現風格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)]> 

謝謝大家。

+0

錯誤提示您嘗試將'alert.view.translatesAutoresizingMaskIntoConstraints'設置爲'NO'。你試過這個嗎? – n00neimp0rtant

+0

是的,我試過 ''[alert.view setTranslatesAutoresizingMaskIntoConstraints:NO];',同樣的問題 –

+1

你有沒有想過這個?我有同樣的問題。 – Fil

回答

-2

我找到了解決自己:

這個來自行:

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

我必須設置sourceView OR sourceRect但不能同時使用。

+0

那麼,我這樣做,以取代我的應用程序(約200-300)的所有alertView&actionSheet,它完美的作品 –

+0

這對我來說不適用於iOS 9。它崩潰並說你需要定義兩個。 – Mike

+1

實際上它們都應該被設置,但應該參考彈出窗口應該出現在其旁邊的視圖/按鈕。 – Fil

2

我在ActionSheet風格的UIAlertController中也遇到類似的問題。

另一個可能的原因是錯誤地設置了的permittedArrowDirections。在我的情況下,popover的sourceView位於頂部,但我已將permittedArrowDirections設置爲UIPopoverArrowDirectionDown,這會導致「無法同時滿足約束」錯誤。將其設置爲UIPopoverArrowDirectionUp後錯誤消失。

0

我遇到了同樣的問題,因爲我最初並不瞭解sourceViewsourceRect必須引用與操作表關聯的按鈕/視圖。這樣彈出窗口就會出現在它旁邊。如果改爲使用整個視圖,則彈出窗口會出現在屏幕之外,這會產生約束錯誤。

相關問題