【问题标题】:Swift - Customizing cell content (UILabel, UIImageView)Swift - 自定义单元格内容(UILabel、UIImageView)
【发布时间】:2019-11-03 12:07:50
【问题描述】:

我想自定义我的UICollectionViewCell,但它不能像我希望的那样工作。

这是我的第一个单元格的代码:

class MainWishlistCell: UICollectionViewCell {
    let wishlistImage: UIImageView = {
        let v = UIImageView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.image = UIImage(named: "logoGroß")
        v.layer.cornerRadius = 3.0
        v.layer.shadowColor = UIColor.black.cgColor
        v.layer.shadowOffset = CGSize(width: 3, height: 3)
        v.layer.masksToBounds = false
        return v
    }()

    let wishlistLabel: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.text = "Main Wishlist"
        v.font = UIFont(name: "Avenir Next-Bold", size: 18)
        v.textColor = .darkGray
        v.textAlignment = .center
        return v
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    func commonInit() -> Void {
        contentView.addSubview(wishlistImage)
        contentView.addSubview(wishlistLabel)
        // constrain view to all 4 sides
        NSLayoutConstraint.activate([
            wishlistImage.topAnchor.constraint(equalTo: contentView.topAnchor),
            wishlistImage.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            wishlistImage.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            wishlistImage.heightAnchor.constraint(equalToConstant:150),

            wishlistLabel.topAnchor.constraint(equalTo: wishlistImage.bottomAnchor,constant: 1),
            wishlistLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
            wishlistLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            wishlistLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
        ])
    }
}

它看起来像这样:

1.问题 - 为什么标签不是bold

2。问题 - 为什么没有cornerRadius

3.问题 - 为什么shadow 没有出现(底部+右侧)?

【问题讨论】:

    标签: ios swift uiimage uilabel uicollectionviewcell


    【解决方案1】:

    对于@pigeon_39 所说的第一个问题,您应该写出没有空格的字体名称。

    对于第二个和第三个问题,你可以使用这个解决方案:

    func addShadow() {
        let cornerRadius: CGFloat = 5
        self.wishlistImage.layer.shadowPath = UIBezierPath(roundedRect: self.wishlistImage.bounds, cornerRadius: cornerRadius).cgPath
        self.wishlistImage.layer.shadowRadius = cornerRadius
        self.wishlistImage.layer.shadowOffset = .zero
        self.wishlistImage.layer.shadowOpacity = 0.3
        self.wishlistImage.layer.shadowRadius = 10
        self.wishlistImage.layer.cornerRadius = cornerRadius
        self.wishlistImage.layer.shadowColor = UIColor.black.cgColor
    }
    

    并在设置约束后调用它。

    【讨论】:

    • 设置约束后是什么意思?在func commonInit() 之后调用addShadow() 对我不起作用。
    • 我的意思是在激活约束后在commonInit 中调用它
    • 对你有用??标签周围有阴影,但图像没有任何反应:D
    • 你确定你对wishListImagewishListLabel 的看法是正确的吗?如您所见,我是为wishListImage 写的!
    • 是的。我将 roundRect: 设置为 self.wishlistImage.bounds 。我刚刚有selfbefore.. 现在它根本不再出现
    【解决方案2】:

    试试这个

    1. 问题:1

    v.font = UIFont(name: "AvenirNext-Bold", size: 18)//删除Avenir和Next之间的空格

    1. 问题:2 & 3

    将此扩展添加到您的项目中

    extension UIImageView {
    
        func addShadow(offset: CGSize, color: UIColor, radius: CGFloat, opacity: Float)
        {
            layer.masksToBounds = false
            layer.shadowOffset = offset
            layer.shadowColor = color.cgColor
            layer.shadowRadius = radius
            layer.shadowOpacity = opacity
        }
    }
    

    然后这样称呼它

    let wishlistImage: UIImageView = {
            let v = UIImageView()
            v.translatesAutoresizingMaskIntoConstraints = false
            v.image = UIImage(named: "logoGroß")
            v.layer.cornerRadius = 3.0
            v.addShadow(offset: CGSize(width: 3, height: 3), color: UIColor.black, radius: 2.0, opacity: 1.0)
            return v
        }()
    

    【讨论】:

    • 除圆角半径外,一切都运行良好。知道如何解决这个问题吗?
    • 圆角设置layer.masksToBound = true,阴影设置false!这不是为圆形图像添加阴影的解决方案。
    • 你知道解决办法吗?
    • 还有一件事,我尝试将字体更改为 medium 而不是 bold 但不知何故 AvenirNext-Medium 不起作用..
    猜你喜欢
    • 2017-06-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多