【问题标题】:Custom Capsule Background with UIImageView programmatically not working?以编程方式使用 UIImageView 自定义胶囊背景不起作用?
【发布时间】:2021-08-15 18:46:07
【问题描述】:

我在 UIImageView 中返回单个 UILabel,在 UICollectionViewController 的单元格中。我的 didSelectItem 和动态宽度或为单元格工作。

但是我试图使 UIImageView 成为胶囊形状,我认为下面的代码会返回一个圆圈,但事实并非如此。我要做的是Text的胶囊背景层,类似于按钮但不是按钮。

class CategoryView: UIView {
    
    private let capsuleBackground: UIImageView = {
        let view = UIImageView()
        view.layer.backgroundColor = UIColor.white.cgColor
        view.layer.borderWidth = 1
        view.layer.borderColor = COLOR_PRIMARY?.cgColor
        view.layer.cornerRadius = view.frame.size.width/2
        view.translatesAutoresizingMaskIntoConstraints = false
        view.clipsToBounds = true
        view.isUserInteractionEnabled = false
        return view
    }()
    
    private let textLabel: CustomUILabel = {
        let label = CustomUILabel(title: "Hello World")
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }
    
    
    fileprivate func setup() {
        addSubview(capsuleBackground)
        capsuleBackground.addSubview(textLabel)
        
        
        capsuleBackground.centerInSuperview()
        textLabel.edges(to: capsuleBackground, insets: TinyEdgeInsets(top: 8, left: 8, bottom: 8, right: 8))
        
    }
}

这就是它的样子

【问题讨论】:

    标签: swift uiimageview uikit


    【解决方案1】:

    view.layer.cornerRadius = 12 ?‍♂️

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多