繁体   English   中英

如何在UITableViewCell中的UIStackView中添加UIView?

[英]How do I add a UIView to a UIStackView in a UITableViewCell?

每当我尝试将图像,gif或视频添加到StackView时,单元格都不会调整大小以适合媒体。 媒体与StackView内部的所有内容重叠。

class HubTableViewCell: UITableViewCell {

@IBOutlet weak var labelContent: UILabel!
@IBOutlet weak var stackViewContainer: UIStackView!

var mediaImage:UIImageView?

override func layoutSubviews() {
    super.layoutSubviews()
    mediaImage = UIImageView(frame: CGRect(x: 0, y: 0, width: stackViewContainer.bounds.width, height: 420))
    mediaImage?.kf_setImageWithURL(NSURL(string: "https://placem.at/people")!)
    mediaImage?.clipsToBounds = true
    mediaImage?.contentMode = .ScaleAspectFill
    mediaImage?.widthAnchor.constraintEqualToConstant(stackViewContainer.bounds.width).active = true
    mediaImage?.heightAnchor.constraintEqualToConstant(420).active = true
    if let mediaImage = mediaImage {
        stackViewContainer.insertSubview(mediaImage, aboveSubview: labelContent)
        stackViewContainer.bringSubviewToFront(mediaImage)
    }
}

override func prepareForReuse() {
    super.prepareForReuse()
    if let mediaImage = mediaImage {
        stackViewContainer.removeArrangedSubview(mediaImage)
        mediaImage.removeFromSuperview()
    }
    mediaImage = nil
}

 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier") as! UITableViewCell
    cell.yourUIStackView.addArrangedSubview(yourView)

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM