自动布局-iTableViewCell内的UILabel在iOS 6的初始加载时未包装
我在自定义的自动布局-iTableViewCell内的UILabel在iOS 6的初始加载时未包装,ios,objective-c,uitableview,cocoa-touch,autolayout,Ios,Objective C,Uitableview,Cocoa Touch,Autolayout,我在自定义的UITableViewCell中看到了UILabel包装的奇怪问题。我使用的是自动布局,虽然它在iOS>6上运行良好,但iOS 6的第一次加载时,UILabel内部的UITableViewCell没有包装。当我点击单元格并显示详细信息视图,然后返回时,UILabel将按预期结束 以下是我如何将标签添加到单元格: self.productNameLabel = [[UILabel alloc] init]; self.productNameLabel.translatesAutores
UITableViewCell
中看到了UILabel
包装的奇怪问题。我使用的是自动布局,虽然它在iOS>6上运行良好,但iOS 6的第一次加载时,UILabel
内部的UITableViewCell
没有包装。当我点击单元格并显示详细信息视图,然后返回时,UILabel将按预期结束
以下是我如何将标签添加到单元格:
self.productNameLabel = [[UILabel alloc] init];
self.productNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.productNameLabel.backgroundColor = [UIColor clearColor];
self.productNameLabel.textAlignment = NSTextAlignmentLeft;
self.productNameLabel.font = kFontDynamicSubHead;
self.productNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.productNameLabel.numberOfLines = 2;
[self.contentView addSubview:self.productNameLabel];
然后我添加一些约束来定位标签。我没有设置任何高度/宽度约束
最后,在我的单元格“layoutSubviews
中,我为标签设置了preferredMaxLayoutWidth
- (void)layoutSubviews {
[super layoutSubviews];
self.productNameLabel.preferredMaxLayoutWidth = self.productNameLabel.bounds.size.width;
}
如果有人以前遇到过这种情况或知道解决方法,请提出建议。尝试在代码中为
UILabel
添加约束,尝试为您的单元格initWithCoder
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
self.productNameLabel = [[UILabel alloc] init];
self.productNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.productNameLabel.backgroundColor = [UIColor clearColor];
self.productNameLabel.textAlignment = NSTextAlignmentLeft;
self.productNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.productNameLabel.numberOfLines = 2;
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:self.productNameLabel];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"view":self.productNameLabel}]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[view]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"view":self.productNameLabel}]];
[self setNeedsLayout];
}
return self;
}
希望这有帮助