程序化NSLayoutConstraint在UITableViewCell类中无法正常工作

时间:2015-03-13 06:54:55

标签: ios swift

我正在尝试构建一个具有可变行高的聊天应用程序。这是我在UIViewController类的viewDidLoad()中放入UITableView的目的:

tblMessages.estimatedRowHeight = 80.0
tblMessages.rowHeight = UITableViewAutomaticDimension

接下来,我创建了一个自定义的UITableViewCell类,其中包含以下代码:

    class MessageTableViewCell: UITableViewCell
{
    var messageBG = UIView()
    var textMessage = UILabel()

    override func awakeFromNib()
    {
        super.awakeFromNib()

        messageBG.backgroundColor = UIColor.grayColor()
        messageBG.frame = CGRectMake(0, 0, 10, 40)
        messageBG.layer.cornerRadius = 20

        self.textMessage.numberOfLines = 0
        self.textMessage.lineBreakMode = NSLineBreakMode.ByWordWrapping

        addSubview(messageBG)
        addSubview(textMessage)
    }

    override func drawRect(rect: CGRect)
    {
        super.drawRect(rect)
        refreshView()
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    func refreshView()
    {
        //NSLayoutConstraint.deactivateConstraints(self.constraints())

        textMessage.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.addConstraint(NSLayoutConstraint(item: textMessage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: textMessage, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 20))
        self.addConstraint(NSLayoutConstraint(item: textMessage, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -50))
        self.addConstraint(NSLayoutConstraint(item: textMessage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 20))
        self.addConstraint(NSLayoutConstraint(item: textMessage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 19))

        self.messageBG.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.addConstraint(NSLayoutConstraint(item: self.messageBG, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: self.messageBG, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 8))
        self.addConstraint(NSLayoutConstraint(item: self.messageBG, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -49))
        self.addConstraint(NSLayoutConstraint(item: self.messageBG, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 6))
        self.addConstraint(NSLayoutConstraint(item: self.messageBG, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 6))
    }
}

在上面,我添加了彩色背景(UIView)和多行UILabel。将长文本传递给行单元格时,我在应用程序启动时得到了这个:http://postimg.org/image/8xvcshkfh/

当我在模拟器中旋转视图时,我有了这个但却有错误的尺寸/约束:http://postimg.org/image/oyl1yyi3h/

有人可以帮我解决我做错的事吗?

1 个答案:

答案 0 :(得分:0)

我最近发现我在代码中犯了一个错误 -

self.addConstraint(..)

应替换为:

self.contentView.addConstraint(..)