我正在嘗試將此tableview部分標題視圖調整爲自動佈局,因此它將適用於iPhone 6和6 Plus。這是所期望的結果 右對齊視圖不工作的自動佈局
具有命名stringView
,favoriteCount
和heartImage
3次。以下屏幕截圖將在每個視圖上設置灰色背景,以便更容易地看到發生了什麼。
我不明白的是,以下
@"|-(15.0)-[stringView]-(>=20.0)-[favoriteCount(44.0)]-[heartImage(22.0)]-(10.0)-|"
產生
與計數和心臟意見不見蹤影。所以感覺好像包含的視圖比它所需要的要寬得多,但我不知道我有任何控制權,因爲從這個方法返回的視圖應該自動按照tableview的寬度調整大小,不應該這樣做?
BTW這個tableView:viewForHeaderInSection:
方法,它看起來像
- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 0) {
// My Favourites menu
UIView *myFavoritesMenuView = [[UIView alloc] init];
[myFavoritesMenuView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:1.0]];
UILabel *stringView = [[UILabel alloc] init];
[stringView setTranslatesAutoresizingMaskIntoConstraints:NO];
[stringView setText:@"My Favourites"];
[stringView setTextColor:[UIColor whiteColor]];
[stringView setFont:[UIFont fontWithName:@"OpenSans" size:16.0]];
[stringView setBackgroundColor:[UIColor grayColor]];
[myFavoritesMenuView addSubview:stringView];
NSDictionary *favoriteArticles = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"FavoriteArticles"];
UILabel *favoriteCount = [[UILabel alloc] init];
[favoriteCount setTranslatesAutoresizingMaskIntoConstraints:NO];
[favoriteCount setFont:[UIFont fontWithName:@"OpenSans" size:16.0]];
[favoriteCount setTextColor:[UIColor whiteColor]];
[favoriteCount setTextAlignment:NSTextAlignmentRight];
[favoriteCount setBackgroundColor:[UIColor grayColor]];
[myFavoritesMenuView addSubview:favoriteCount];
if (favoriteArticles && favoriteArticles.count > 0) {
[favoriteCount setText:[NSString stringWithFormat:@"%lu", (unsigned long)favoriteArticles.count]];
} else {
[favoriteCount setText:@""];
}
UIImageView *heartImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"heart-button-on"]];
[heartImage setTranslatesAutoresizingMaskIntoConstraints:NO];
[heartImage setBackgroundColor:[UIColor grayColor]];
[myFavoritesMenuView addSubview:heartImage];
// Create the views dictionary
NSDictionary *views = NSDictionaryOfVariableBindings(stringView, favoriteCount, heartImage);
// Horizontal layout - note the options for aligning the vertical center of all views
NSString *horizontalFormat = @"|-(15.0)-[stringView]-(>=20.0)-[favoriteCount(44.0)]-[heartImage(22.0)]-(10.0)-|";
[myFavoritesMenuView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:horizontalFormat
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:views]];
// Vertical layout - we only need one "column" of information because of the alignment options used when creating the horizontal layout
NSString *verticalFormat = @"V:|-[stringView]-|";
[myFavoritesMenuView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:verticalFormat
options:0
metrics:nil
views:views]];
return myFavoritesMenuView;
}
// handle other section headers here
}
順便內部所有發生的事情,我也通過創建一個UIView作爲單獨的廈門國際銀行試過,只包含文本標籤左對齊和心臟圖像對齊到超視圖的後緣,並且看到與心臟完全相同的問題不可見(可能是在屏幕右側)。
是否在代碼中創建了標題視圖?確保你調用[myHeaderView setTranslatesAutoresizingMasksIntoConstraints:NO]。 – 2014-10-29 14:03:26
顯示你在tableView:viewForHeaderInSection中的完整代碼:所以我們可以看到你是如何創建這個視圖的。您顯示的限制條件應該可以正常工作(假設您也有正確的垂直限制)。 – rdelmar 2014-10-29 23:32:23
@MikeTaverne是在代碼中創建標題視圖。如果我調用[myFavoritesMenuView setTranslatesAutoresizingMaskIntoConstraints:NO];那麼這個容器視圖甚至不會出現在表格中。 – martinjbaker 2014-10-30 06:13:52