我正在尝试将此tableview部分标题视图调整为自动布局,以便适应iPhone 6和6 Plus。这是期望的结果
有3个名为stringView
,favoriteCount
和heartImage
的视图。以下屏幕截图将在每个视图上设置灰色背景,以便更容易查看正在进行的操作。
我不明白的是以下
@"|-(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作为单独的xib创建,只包含左边对齐的文本标签和与superview后缘对齐的心脏图像,并且看到心脏不可见的完全相同的问题(可能是关闭的)屏幕右侧)。
答案 0 :(得分:0)
整个表格视图的宽度不正确(表格视图由于某种原因是正方形而不是适合屏幕)所以缺少的子视图在屏幕右侧。当我在横向视图中更改为在iPhone 6中运行并且子视图出现时发现了它......