2014-06-24 57 views
1

我有在代碼插入myController的,(自=根視圖控制器)這樣:定製自動佈局約束添加控制器的視圖時破碎編程

MyViewController* myController = [[MyViewController alloc] initWithNibName:nil bundle:nil]; 
    [self addChildViewController:myController]; 
    [self.view addSubview:myController.view]; 
    myController.view.frame = CGRectMake(0, 504, 320, 216); 

在MyViewController的XIB文件,我只有2個視圖,一個在另一個之上。可以說頂視圖叫做View1,底視圖是View2。 View0是主視圖。

我已加入下列垂直NSLayoutConstraints: 五:[視圖2(40)] 五:| - (0)-View1(產品名稱: '|':View0) 五:View2-(0) - | (名稱:'|':View0) V:View1-(0)-View2

也就是說View1在頂部觸及View0,在底部觸摸View2。 View2在頂部觸及View1,在底部觸摸View0,並且具有40的恆定高度。

當我以縱向方式運行這個時,每件事情似乎都沒問題。但是當我旋轉到風景時,我有時會出現以下錯誤。

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) 
(
"<NSAutoresizingMaskLayoutConstraint:0xd17cc00 h=--& v=--& V:[UIView:0xd174a30(268)]>", 
"<NSLayoutConstraint:0x9469890 V:[View2(40)]>", 
"<NSLayoutConstraint:0x9468e90 V:|-(0)-View1 (Names: '|':View0)>", 
"<NSLayoutConstraint:0x9468ef0 V:View2-(0)-| (Names: '|':View0)>", 
"<NSLayoutConstraint:0x94676f0 V:View1-(0)-View2>", 
"<NSAutoresizingMaskLayoutConstraint:0x9450640 h=-&- v=-&- UIView:0000.height == UIView:0xd174a30.height - 268>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x9469890 V:[View2(40)]> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

有人明白爲什麼會發生這種情況嗎?我應該怎麼做才能將我定義的約束保持爲先前的?

回答

5

好了,所以最後,爲了解決這個問題,我必須在View0上設置translatesAutoresizingMaskIntoConstraints爲NO。

然後,爲了很好地顯示我的View0,我必須爲View0手動創建NSLayoutConstraint並將這些約束添加到View0的超級視圖。

0

自動調整掩碼是在iOS中執行佈局的傳統方式(在iOS 6中進行自動佈局之前)。在UIView上有一個屬性,它自動將此遺留系統轉換爲佈局約束,稱爲translatesAutoresizingMaskIntoConstraints,默認爲YES。如果您在代碼中創建視圖(或者在某些情況下使用IB)並且打算使用自動佈局來定位該視圖,則應該將此屬性設置爲NO以避免出現上述問題。無論何時在約束警告中看到那些約束類型,這都可能是原因。

相關問題