【发布时间】:2018-09-19 11:39:46
【问题描述】:
我有一个按钮放在自定义 UICollectionViewCell 的顶角。出于某种原因,它看起来是半透明的,因此当我不希望出现这种情况时,通过按钮显示单元格的边框。
在我的 CustomCollectionviewCell 中:
lazy var favoriteButton: UIButton = {
let button = UIButton(type: .system)
button.setImage(#imageLiteral(resourceName: "star_white").withRenderingMode(.alwaysOriginal), for: .normal)
button.addTarget(self, action: #selector(favoriteTapped), for: .touchUpInside)
button.isEnabled = true
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.mainWhite()
layer.borderWidth = 1.0
layer.borderColor = UIColor.lightGray.cgColor
setupCellLayout()
}
fileprivate func setupCellLayout() {
addSubview(favoriteButton)
favoriteButton.translatesAutoresizingMaskIntoConstraints = false
favoriteButton.heightAnchor.constraint(equalToConstant: 32).isActive = true
favoriteButton.widthAnchor.constraint(equalToConstant: 32).isActive = true
favoriteButton.centerXAnchor.constraint(equalTo: rightAnchor, constant: -12).isActive = true
favoriteButton.centerYAnchor.constraint(equalTo: topAnchor).isActive = true
bringSubview(toFront: favoriteButton)
}
这会导致以下结果:
这些图像都不是半透明的。他们有坚实的背景,所以我很困惑为什么他们在应用程序中显示为半透明。我最初认为它们可能被放置在单元格后面,所以我在 setupCellLayout() 中添加了对 bringSubview(toFront: favoriteButton) 的调用,但这并没有解决问题。
我还认为使用.withRenderingMode(.alwaysOriginal) 应该可以解决问题,但没有运气。
关于为什么会发生这种情况的任何想法?
【问题讨论】:
-
向 UICollectionViewCell 的 contentView 属性添加视图
-
你有alpha小于1的view吗
-
@Shruti 不,我没有
-
@aleixrr 这并不能解决问题。实际上没有任何变化
标签: ios uibutton uicollectionviewcell