I have a button that working fine when his alpha is 1, but when I set the alpha to 0, it doesn't do the functions. (I want the button to be invisible, but still responsive). how do I fix that?
thanks!
I have a button that working fine when his alpha is 1, but when I set the alpha to 0, it doesn't do the functions. (I want the button to be invisible, but still responsive). how do I fix that?
thanks!
Setting the alpha to 0, will disable the UIView and no touch events will be received, so you need to set it to 0.02 at least.
Source is this answer on SO : Does UIButton become disabled when its alpha is set to 0.0?
According to Apple Docs
This method ignores view objects that are hidden, that have disabled user interactions, or have an alpha level less than 0.01. This method does not take the view’s content into account when determining a hit. Thus, a view can still be returned even if the specified point is in a transparent portion of that view’s content.
Any UIView that has alpha lower than 0.01 will be ignored by the touch events processing system, i.e. will not receive touch.
Set it 0.02 at least
Setting the alpha of a UIButton to zero will not allow getting any touch events from it.
Instead, you can keep the alpha 1.0 and set the background color to UIColor.clear, and the text to "" (empty). This way, the button will be invisible like you asked, but still tappable.
you can add another view above your button and make it a clear color so that it will be the same as your background color and hide your button with alpha 0 easily and add a tap gesture to the view you added above the button so that when you tap on the view it will perform an action
let tap = UITapGestureRecognizer(target: self, action:#selector(self.handleTap(_:)))
aboveButtonView.addGestureRecognizer(tap)
aboveButtonView.isUserInteractionEnabled = true
self.view.addSubview(aboveButtonView)
// function which is triggered when handleTap is called
@objc func handleTap(_ sender: UITapGestureRecognizer) {
// do action needed
}