我對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 theUIView
propertytranslatesAutoresizingMaskIntoConstraints
)"
基本上我想要做的是在這裏輸入的代碼有兩個標籤相互齊平,併爲他們重新大小根據方向/設備(我將他們設置一個背景顏色,所以希望他們看起來'連續')
任何人都可以幫我解釋我失蹤了嗎?提前致謝。
我添加標籤的代碼是:
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];
您的'updateConstraints'方法和'layoutSubviews'方法會發生什麼?你打電話給'[super layoutSubviews]'? – Evan
嗯,根據這個:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AdoptingAutoLayout/AdoptingAutoLayout.html你不應該調用setTranslatesAutoresizingMaskIntoConstraints:NO(見文章的最後一段) – Yohst
謝謝@Evan,但我不覆蓋我創建的自定義單元類中的這些方法,因此我不知道錯誤在哪裏得到.. – vica