I am trying to hide a uiview but still have it receive touch events. I am setting the alpha to 0 like so:
mainView.alpha = 0.0
also I've tried setting this to true but it doesn't do anything
mainView.userInteractionEnabled = true
Trouble is it no longer receives touch events when I do this. How can I enable it to receive touch events but be effectively hidden?
Set your UIView
's backgroundColor
to UIColor.clear
.
mainView.backgroundColor = .clear
There are two ways to handle this:
The hard one - instead of setting alpha on the view, set it on all its subviews and make all the content in the view invisible (e.g. change background to clear color).
The easy one - let another view handle the events. Just add another transparent view with the same position and size (which is easy using constraints) which will handle the events.
By the way, if you check the documentation for method hitTest:withEvent: which is used to handle touch events, it says that views with alpha
lower than 0.01
won't receive touches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With