AutoLayout(cell自适应)

作者: guaker | 来源:发表于2016-06-02 18:17 被阅读197次

今天在写项目的时候遇到一个坑,计算cell高度的时候是英文的时候会出现高度不够的情况


cell

代码是这么写的

let content = comment.content as NSString
let rect = content.boundingRectWithSize(
    CGSizeMake(screenWidth - 50, CGFloat.max),
    options: [.UsesLineFragmentOrigin, .UsesFontLeading],
    attributes: [NSFontAttributeName: font13pt],
    context: nil)
    
    return rect.height + 55

网上查了boundingRectWithSize各种问题也没能解决,最后想到了使用AutoLayout实现cell的自适应,iOS7的方法这里就不说明了,iOS8只需要一句代码

self.tableView.estimatedRowHeight = 80

不过需要注意的是AutoLayout的设置,要让contentView知道怎么撑开,所以需要设置上下边界的约束。之前代码:

self.contentView.addConstraints(
    NSLayoutConstraint.constraintsWithVisualFormat(
        "V:|-13-[nameLabel]-9-[contentLabel]",
        options: NSLayoutFormatOptions(),
        metrics: nil,
        views: viewsDictionary))

需要加上下边界的约束

self.contentView.addConstraints(
    NSLayoutConstraint.constraintsWithVisualFormat(
        "V:|-13-[nameLabel]-9-[contentLabel]-10-|",
        options: NSLayoutFormatOptions(),
        metrics: nil,
        views: viewsDictionary))
cell

相关文章

网友评论

本文标题:AutoLayout(cell自适应)

本文链接:https://www.haomeiwen.com/subject/dzskdttx.html