【发布时间】:2014-09-23 10:09:56
【问题描述】:
好的,我不知道 iOS 8 发生了什么,但是当我在它上面运行我的应用程序时,我看到了这些额外的 UITableViewSeparatorViews:
更新
我使用了 Xcode 6 视图调试器,我看到在这个有问题的 UITableView 中,我看到这些额外的视图被添加为我的 UITableViewCellContentView 的子视图
_UITableViewCellSeparatorView
为什么 iOS 8 将这些随机子视图添加到我的单元格的 contentView 中?
从屏幕截图中可以看出,那些多余的线条是那些没有到达屏幕边缘的线条。
在我的视图控制器中,我使用以下代码使线条一直延伸到屏幕边缘:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
在我的自定义单元类中,我正在设置:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_reuseID = reuseIdentifier;
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.contentView.backgroundColor = [[BSGThemeManager sharedTheme] clearColor];
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.containerView = [[UIView alloc] init];
_photo = [[BSGUserPhotoView alloc] init];
[_photo clipPhotoToRadius:37.5];
//_photo.layer.cornerRadius = 37.5 / 2.0;
//_photo.layer.borderColor = [[BSGThemeManager sharedTheme] photoBorderColor];
//_photo.layer.borderWidth = [[BSGThemeManager sharedTheme] photoBorderWidth];
//_photo.clipsToBounds = YES;
self.photo.alpha = 0;
_message = [[UILabel alloc] init];
_message.backgroundColor = [[BSGThemeManager sharedTheme] clearColor];
_message.numberOfLines = 0;
_message.lineBreakMode = NSLineBreakByWordWrapping;
_message.font = [[BSGThemeManager sharedTheme] varelaRoundFontWithSize:15];
self.message.alpha = 0;
_time = [[UILabel alloc] init];
_time.textAlignment = NSTextAlignmentRight;
_time.font = [[BSGThemeManager sharedTheme] helveticaNeueFontWithSize:16];
_time.textColor = [[BSGThemeManager sharedTheme] postTimeColor];
[_time setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[_time setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
self.time.alpha = 0;
[self.containerView addSubview:_photo];
[self.containerView addSubview:_message];
[self.containerView addSubview:_time];
[self.contentView addSubview:self.containerView];
[self addAllConstraints];
}
return self;
}
我不确定这条线是否是它的原因:
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
我的单元格的自动布局代码是:
-(void)addAllConstraints
{
self.containerView.translatesAutoresizingMaskIntoConstraints = NO;
self.photo.translatesAutoresizingMaskIntoConstraints = NO;
self.message.translatesAutoresizingMaskIntoConstraints = NO;
self.time.translatesAutoresizingMaskIntoConstraints = NO;
id views = @{
@"containerView": self.containerView,
@"photo": self.photo,
@"message": self.message,
@"time": self.time
};
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView]|" options:0 metrics:nil views:views]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|" options:0 metrics:nil views:views]];
// --------------------------------------------------
// Horizontal Constraints
// --------------------------------------------------
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[photo(37.5)]-15-[message(195)]" options:0 metrics:nil views:views]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[time]-10-|" options:0 metrics:nil views:views]];
// --------------------------------------------------
// Vertical Constraints
// --------------------------------------------------
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[photo(37.5)]" options:0 metrics:nil views:views]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.photo attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.message attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.photo attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.time attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.photo attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
还有其他人遇到过这个奇怪的 iOS 8 错误吗?
它不会在 iOS 7 上显示。
【问题讨论】:
-
您的回答对我不起作用。还有其他解决方案吗?
-
不知道,控制台输出中是否有任何冲突的约束?就我而言,我首先确保我没有任何冲突的约束。也许在您的源代码中发布另一个 Stackoverflow 问题,以便其他人可以看到并希望提供解决方案?
-
已解决。在下面检查我的答案。谢谢:)
标签: ios uitableview autolayout ios8