2013-12-10 56 views
0

我對iOS編程相當陌生,可能不理解視圖層次結構以及我應該這樣做,因此無法在自定義表格單元類中成功獲取兩個標籤,這些標籤是爲了正確自動調整大小而創建的。即"translatesAutoresizingMaskIntoConstraints"屬性讓我有點困惑。AutoLayout TableViewCell中的兩個標籤?

我沒有爲這部分代碼使用storyboard:我有一個TableViewController,我在viewDidLoad中創建了我自己的tableView。在cellForRowAtIndexPath我啓動我自己的TableViewCell實現。

我遇到的問題是,當我設置"setTranslatesAutoresizingMaskIntoConstraints"NO爲表視圖,然後我創建UILabels加我的限制,我得到以下錯誤:如果我註釋掉

"Terminating app due to uncaught exception `'NSInternalInconsistencyException',` reason: 'Auto Layout still required after executing `-layoutSubviews`. UITableView's implementation of `-layoutSubviews` needs to call super.'" 

setTranslatesAutoresizingMaskIntoConstraints線,我的應用程序運行,但我得到有關限制以下警告:

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

基本上我想要做的是在這裏輸入的代碼有兩個標籤相互齊平,併爲他們重新大小根據方向/設備(我將他們設置一個背景顏色,所以希望他們看起來'連續')

任何人都可以幫我解釋我失蹤了嗎?提前致謝。

我添加標籤的代碼是:

self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 30.0f)]; 
self.nameLabel.textColor = [UIColor redColor]; 
self.nameLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:12.0f]; 
self.nameLabel.backgroundColor = [UIColor brownColor]; 
[self.nameLabel setText:@"Test"]; 
// [self.nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self.contentView addSubview:self.nameLabel]; 

...

NSDictionary *viewsDictionary = 
NSDictionaryOfVariableBindings(nameLabel, summaryLabel); 
NSArray *constraints = 
[NSLayoutConstraint constraintsWithVisualFormat:@"|-[nameLabel][summaryLabel]-|" 
             options:0 
             metrics:nil 
              views:viewsDictionary]; 
+0

您的'updateConstraints'方法和'layoutSubviews'方法會發生什麼?你打電話給'[super layoutSubviews]'? – Evan

+0

嗯,根據這個:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AdoptingAutoLayout/AdoptingAutoLayout.html你不應該調用setTranslatesAutoresizingMaskIntoConstraints:NO(見文章的最後一段) – Yohst

+0

謝謝@Evan,但我不覆蓋我創建的自定義單元類中的這些方法,因此我不知道錯誤在哪裏得到.. – vica

回答

1

I'm fairly new to ios programming and probably don't understand the view hierarchy as well as I should and thus am failing to successfully get two labels within a custom table cell class I have created to autoresize properly. Namely the "setTranslatesAutoresizingMaskIntoConstraints" property has me a little confused.

好,translatesAutoresizingMaskIntoConstraints是蘋果公司創建,使過渡從Autoresizing (Spring and Struts)Autolayout容易的屬性。說,你有一些AutoresizingMasks爲您的觀點,你只需切換Autolayout ON沒有設置任何約束。然後,您現有的AutoresizingMasks將轉換爲約束,以保持視圖。因此,默認translatesAutoresizingMaskIntoConstraints屬性設置爲YES。但是,當您開始添加約束時,在90%的情況下,它們將與通過轉換AutoresizingMasks而創建的約束髮生衝突。所以,最好是通過設置view.translatesAutoresizingMaskIntoConstraints = NO

在你的代碼,將其關閉,下面可能已經創建的問題:


  • Frame

設置您不應將幀設置爲您將要添加約束條件的對象。這是一個範式轉變。當你在Autolayout方式中考慮時,幀只是設置正確約束的結果,它們組合地確定了所討論的視圖的幀。 所以,請刪除框架設置。

self.nameLabel = [[UILabel alloc] init];就足夠了。

  • 設置適當的限制

代碼:


NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(nameLabel, summaryLabel); 
NSArray *constraints = 
[NSLayoutConstraint constraintsWithVisualFormat:@"|-[nameLabel][summaryLabel]-|" 
             options:0 
             metrics:nil 
              views:viewsDictionary]; 
  • NameLabel

    • 現在,上面的約束告訴我們nameLabel水平(如你沒有提到H:或V :)間隔「標準」的距離(20像素)從容器,鄰近所述summaryLabel。 但是它的Y位置,寬度和高度呢? 所以我們需要更多的約束。
  • summaryLabel

    • 同樣適用於summaryLabel

所以,讓我們正確地定義他們:

NSDictionary *viewsDictionary = 
NSDictionaryOfVariableBindings(nameLabel, summaryLabel); 
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[nameLabel(100)][summaryLabel]-|" options:0 metrics:nil views:viewsDictionary]; 
NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[nameLabel(30)]" options:0 metrics:nil views:viewsDictionary]; 
NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[summaryLabel]" options:0 metrics:nil views:viewsDictionary]; 
NSArray *constraints3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[nameLabel(==summaryLabel)]" options:0 metrics:nil views:viewsDictionary]; 


[self.view addConstraints:constraints]; 
[self.view addConstraints:constraints1]; 
[self.view addConstraints:constraints2]; 
[self.view addConstraints:constraints3]; 

現在,您的意見將看起來很好。


在任何時間點,以檢查是否你的意見缺少任何限制,暫停調試器,請在控制檯下面

po [[UIWindow keyWindow] _autolayoutTrace] 

它會顯示您的哪些意見有AMBIGUOUS LAYOUT

此外,請記住在Storyboard/IB中,如果約束顯示爲「橙色」顏色,則需要更多約束來定義對象位置。一旦添加了所有必要的約束條件,限制顏色變爲「藍色」

0

首先,你在創建後添加約束self.contentView?其次,也許你的約束集不適合自動佈局,並且它會根據自動調整掩碼創建自己的約束。嘗試爲標籤添加垂直約束和寬度約束。