以编程方式使用 UIImageView 的自定义胶囊背景不起作用?
Custom Capsule Background with UIImageView programmatically not working?
我正在 returnUIImageView 中的单个 UILabel,在 UICollectionViewController 的 Cell 中。我的 didSelectItem 和动态宽度或为单元格工作。
但是我试图使 UIImageView 成为胶囊形状,我认为下面的代码会 return 一个圆但不是。我想做的是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))
}
}
这是它的样子
view.layer.cornerRadius = 12
♂️
我正在 returnUIImageView 中的单个 UILabel,在 UICollectionViewController 的 Cell 中。我的 didSelectItem 和动态宽度或为单元格工作。
但是我试图使 UIImageView 成为胶囊形状,我认为下面的代码会 return 一个圆但不是。我想做的是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))
}
}
这是它的样子
view.layer.cornerRadius = 12 ♂️