Friday, 21 September 2018

UIButton is transparent when I don't want it to be

I have a button that I'm placing at the top corner of a custom UICollectionViewCell. For some reason, it is appearing translucent so that the border of the cell is being shown through the button when I would not like that to be the case.

In my 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)
    }

This results in the following:

enter image description hereenter image description here

Neither of these images are translucent. They have solid backgrounds so I am confused as to why they are displaying as translucent in the app. I originally thought that maybe they were being placed behind the cell so I added that call to bringSubview(toFront: favoriteButton) in setupCellLayout() but that didn't fix it.

I also thought that using .withRenderingMode(.alwaysOriginal) should have done the trick, but no luck.

Any Ideas as to why this could be happening?



from UIButton is transparent when I don't want it to be

No comments:

Post a Comment