簡體   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