I've created a custom class that adds a badge to a UIButton. The badge itself is a UIImageView. The button can have a different UI for each state; when the button is disabled it has a clear background, it shows a border and the title, as shown in the image below.
As you can see the badge is displayed behind the border, but I want to badge to be on top of it. I have tried to adjust the zPosition of the layer of the badge by setting it to e.g. 9999. I also tried to bring the badge to the front by using the bringSubviewToFront method. Both ways did not work. My code to add the badge to the button is this:
private func addBadgeImageToButton() {
// badgeImage is a string containing the imageName
guard badgeImage != nil else {
return
}
badgeImageView = UIImageView(image: UIImage(named: badgeImage!))
// This line merely adjusts the position of the UIImageView
badgeImageView!.frame = adjustedRectangleForBadge(initialSize: badgeImageView!.frame.size)
badgeImageView!.layer.zPosition = 9999
addSubview(badgeImageView!)
// This line does not seem to work
//self.bringSubviewToFront(badgeImageView!)
// Adding these two lines won't do the trick either
self.setNeedsLayout()
self.layoutIfNeeded()
}
Can anyone help me with this tiny problem? Any help is greatly appreciated!