Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 标签文本居中,并根据下面标签中的文本行数移动_Swift_Uilabel_Constraints - Fatal编程技术网

Swift 标签文本居中,并根据下面标签中的文本行数移动

Swift 标签文本居中,并根据下面标签中的文本行数移动,swift,uilabel,constraints,Swift,Uilabel,Constraints,我的UILabel固定在单元格顶部,就像UIImage一样,但如果下面的文本有f.e.1行(也在UILabel中),我的顶部UILabel位于其他位置。 这是我的约束和标签声明。感谢所有试图解决我的问题的人:)我尝试在UILabel上使用sizeToFit方法 var albumImage: UIImageView = { let view = UIImageView() view.translatesAutoresizingMaskIntoConstraints = false

我的UILabel固定在单元格顶部,就像UIImage一样,但如果下面的文本有f.e.1行(也在UILabel中),我的顶部UILabel位于其他位置。 这是我的约束和标签声明。感谢所有试图解决我的问题的人:)我尝试在UILabel上使用sizeToFit方法

var albumImage: UIImageView = {
    let view = UIImageView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.contentMode = .scaleAspectFit
    return view
}()

var albumName: UILabel = {
    let view = UILabel()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.numberOfLines = 0
    view.sizeToFit()
    return view
}()

var bandName: UILabel = {
    let view = UILabel()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.sizeToFit()
    return view
}()

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

required init?(coder aDecoder: NSCoder) {
    fatalError("ERROR")
}



func drawLayout(){

    let guide = self.safeAreaLayoutGuide


    //albumImage
    addSubview(albumImage)
    addSubview(albumName)
    addSubview(bandName)

    albumImage.heightAnchor.constraint(equalToConstant: 50).isActive = true
    albumImage.widthAnchor.constraint(equalToConstant: 50).isActive = true
    albumImage.leadingAnchor.constraint(equalTo: guide.leadingAnchor, constant: 8).isActive = true
    albumImage.topAnchor.constraint(equalTo: self.topAnchor, constant: 8).isActive = true
    albumImage.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -8).isActive = true

    //albumTitle
    albumName.topAnchor.constraint(equalTo: bandName.bottomAnchor).isActive = true
    albumName.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -8).isActive = true
    albumName.leadingAnchor.constraint(equalTo: albumImage.trailingAnchor, constant: 8).isActive = true
    albumName.trailingAnchor.constraint(equalTo: guide.trailingAnchor, constant: -8).isActive = true

    //bandName
    bandName.topAnchor.constraint(equalTo: albumImage.topAnchor).isActive = true
    bandName.leadingAnchor.constraint(equalTo: albumImage.trailingAnchor, constant: 8).isActive = true
}

图像在哪里?我在UIViewController中设置了图像,在UITableView中,图像显示良好,具体取决于单元格的高度