Reputation: 1
Thanks for reading.
My program adds UIImageViews upon tapping inside a UICollectionViewCell. Once the image is added, there is a separate, static UIButton set to delete any image(s) which intersect the button. The deleting code works fine, but the problem is that the button is not clickable under the programatically-added UIImageView. (If I click on a portion of the button which is not covered by the UIImageView, the button's function is called and the image(s) are properly deleted.)
Edit: I move the added image views around the screen using a UIGestureRecognizer and the handlepan function.
I have tried view.bringSubviewToFront(UIButton). After doing this, the button worked fine, but now the button was over the UIImageView, so I couldn't move the image outside of the button.
Edit 2: I found a workaround which bypasses the button functionality all together and simply deletes the image if its view intersects the button view at the end of the pan gesture.
//I added this code to the end of the handlepan function (and made a function calleddeleteimage which performs the actions of delete image):
if recognizer.state == UIGestureRecognizerState.Ended { calleddeleteimage() }
//Here is my original code for the UIPanGestureRecognizer and for the UIButton:
func handlepan(recognizer: UIPanGestureRecognizer) { let movingview = recognizer.view! let translation = recognizer.translationInView(recognizer.view) view.bringSubviewToFront(movingview) movingview.center = CGPoint(x: movingview.center.x + translation.x, y: movingview.center.y + translation.y) recognizer.setTranslation(CGPointZero, inView: recognizer.view) view.bringSubviewToFront(hungryman) } @IBOutlet weak var hungryman: UIButton! @IBAction func deleteimage(sender: AnyObject) { var count = 0 indexestodelete = [] for i in unlockeddisplaying { if i.frame.intersects(hungryman.frame) { i.removeFromSuperview() indexestodelete.append(count) } count = count + 1 } count = 0 for i in indexestodelete { unlockeddisplaying.removeAtIndex(i - count) unlockeddisplayingtypes.removeAtIndex(i - count) count = count + 1 } }
Thanks!
TL;DR: How to make UIButton always clickable, even when hidden under UIImageView?
Upvotes: 0
Views: 2366
Reputation: 2782
On your image view, set userInteractionEnabled
to false
and touches will pass through it.
Upvotes: 2