Autolayout - 当一个视图具有动态高度时,在UITableViewCell中垂直居中两个视图

时间:2015-04-09 05:26:47

标签: ios uitableview swift autolayout visual-format-language

target

这就是我想要实现的目标。在UITableView Cell内部,我有一个填充了动态内容的UIlabel和一个具有设置高度的单独UIView。在某些情况下,当有大量文本时,UIlabel会确定UITableView Cell的高度。在其他情况下,当只有一行文本填充UILabel时,UIView设置单元格的高度。 (这就是我现在所拥有的。)

    self.myLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.myView.setTranslatesAutoresizingMaskIntoConstraints(false)


    let viewsDictionary = [
        "myLabel":myLabel,
        "myView":myView,
    ]


    let myLabel_H:Array = NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[myLabel]-60-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
    let myLabel_V:Array = NSLayoutConstraint.constraintsWithVisualFormat("V:|-[myLabel]-|", options:NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)

    self.contentView.addConstraints(myLabel_H)
    self.contentView.addConstraints(myLabel_V)


    let myView_H:Array = NSLayoutConstraint.constraintsWithVisualFormat("H:[myView(50)]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
    let myView_V:Array = NSLayoutConstraint.constraintsWithVisualFormat("V:|-(>=10)-[myView(100)]-(>=10)-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)

    self.contentView.addConstraints(myView_H)
    self.contentView.addConstraints(myView_V)

相反,我得到了这个:(我正在抛出错误:= 10) - | (名称:'|':UITableViewCellContentView:0x7fc21bc6d050)>)

current

2 个答案:

答案 0 :(得分:2)

您需要使用非可视化自动布局格式。

在ObjC中,它看起来像这样:

[self.viewOuterView addConstraint:[NSlayoutConstraint constraintWithItem:self.innerView attribute:NSLayoutAttributeCenterY relateBy:NSLayoutRelationEqual toItem:self.outerView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];

答案 1 :(得分:0)

您可以使用可视布局方法(.AlignAllCenterY)的options参数中的对齐值对齐您的两个子视图,因此您不需要为图像视图添加单独的约束以使其居中在y方向。此外,最好使水平约束从左边缘到标签到图像视图到右边缘,而不是将标签固定到超视图的两个边缘。

class RDTableViewCell: UITableViewCell {

    var myLabel = UILabel()
    var myView = UIImageView()

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        myLabel.numberOfLines = 0
        myLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
        myView.setTranslatesAutoresizingMaskIntoConstraints(false)

        contentView.addSubview(myLabel)
        contentView.addSubview(myView)

        let viewsDictionary = ["myLabel":myLabel, "myView":myView]

        let horizontalCons = NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[myLabel]-[myView(50)]-|", options: .AlignAllCenterY, metrics: nil, views: viewsDictionary)
        let myLabel_V = NSLayoutConstraint.constraintsWithVisualFormat("V:|-[myLabel]-|", options:NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
        let myView_V = NSLayoutConstraint.constraintsWithVisualFormat("V:|-(>=10)-[myView(100)]-(>=10)-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)

        contentView.addConstraints(horizontalCons)
        contentView.addConstraints(myLabel_V)
        contentView.addConstraints(myView_V)
    }
}