jogendra/BadgeHub

Badge hiding behind view.

Harryjeffs opened this issue · 6 comments

I am trying to append badgehub onto a UIImageView. The badge is appearing however when it reaches the corner; it stays behind the imageview or view and just hides away.

I tried setting the z-index however that doesn't seem to be affecting anything. Anyone else having this issue? Or found a solution. Cheers

let hub = BadgeHub(view: cell.contentView) // Initially count set to 0
hub.hubView?.layer.zPosition = 10000
hub.increment()

I'm having the same issue ... did you find a fix @Harryjeffs ?

I tried
self.view.bringSubviewToFront(button2Image)
where button2Image is the image that the BadgeHub is attached to. Same behaviour as you reported.

@lcolli98 Hey! I posted to stackoverflow, and found the answer myself - https://stackoverflow.com/questions/61298622/uicollectionviewcell-badge-masked-behind-view.

Issue was how I was rounding the corners. Let me know if this helps you too :)

That isn't working for me ... any ideas?

button2Image is a UIImageView on top of a UIButton, button2. The badge is being appended to the image view. I've tried manually changing the Z position, ensuring it is first in the storyboard, and bringSubviewToFront, but still no luck.

The image view doesn't have rounded corners (the button does, but the badge should easily be contained within the button).

@lcolli98 Mind sharing some code so we can help debug?

@IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    @IBOutlet weak var button2Image: UIImageView!
    @IBOutlet weak var button3: UIButton!
    @IBOutlet weak var button4: UIButton!
    @IBOutlet weak var userAccButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set the title text based on whether logged in or not
        /*
         if (loggedIn) {
            titleLabel.text = "Welcome back, <name>"
         } else {
            titleLabel.text = "Welcome to AeroBuddy"
         }
         */
        
        // Setup user account button
        userAccButton.layer.cornerRadius = 0.5 * userAccButton.bounds.size.width
        userAccButton.clipsToBounds = true
        userAccButton.layer.borderWidth = 2
        userAccButton.layer.borderColor = UIColor.lightGray.withAlphaComponent(0.6).cgColor
        
        // Set the news notifcation bubble - see https://iosexample.com/a-way-to-quickly-add-a-notification-badge-icon-to-any-view/
        // Use newsBubble.increment(), newsBubble.decerement(), newsBubble.setCount(newCount: Int) to control
        self.view.bringSubviewToFront(button2Image)
        let newsBubble = BadgeHub(view: button2Image)
        if (false /* no unread news */) {
            newsBubble.setCount(0)
        } else {
            // Set the amount of unread news articles
            newsBubble.setCount(1)
            //self.view.bringSubviewToFront(button2Image)
            newsBubble.bump()
        }
    }

image
That's the view controller where everything is displayed.

For anyone else with this issue ... I added a transparent UIView over the top of the button and added the BadgeHub to that. This works.