I’m having a weird UIKit problem. I have a bunch of views in a UIScrollView
and I add a UIContextMenuInteraction
to them when the view is first loaded.
The interaction works great for any of the views that are initially on-screen, but if I scroll to reveal new subviews, the context menu interaction has no effect for those.
I used Xcode's View Debugger to confirm that my interaction is still saved in the view's interactions
property, even for views that were initially off-screen and were then scrolled in.
What could be happening here?
I’m having a weird UIKit problem. I have a bunch of views in a UIScrollView
and I add a UIContextMenuInteraction
to them when the view is first loaded.
The interaction works great for any of the views that are initially on-screen, but if I scroll to reveal new subviews, the context menu interaction has no effect for those.
I used Xcode's View Debugger to confirm that my interaction is still saved in the view's interactions
property, even for views that were initially off-screen and were then scrolled in.
What could be happening here?
Share Improve this question asked Mar 18 at 16:12 BillBill 45.5k25 gold badges127 silver badges218 bronze badges 2- Quick testing... I can't reproduce the issue. You may need to put together a minimal reproducible example? – DonMag Commented Mar 19 at 14:18
- @DonMag I struggled to make one, which led me to investigate my actual app more closely. I found the answer, and recorded it below, in case anyone else has the same issue. Thanks for taking the time to try to reproduce it! – Bill Commented Mar 20 at 17:57
1 Answer
Reset to default 0In case anyone faces the same problem in the future, I found the problem. The scroll view contains a inner parent view, which contains all of the image views. Another part of the code was incorrectly setting the size of that inner view to be the same as the scroll view's bounds.
Because clipsToBounds
is off by default, the images were showing up as you scrolled with no problem. For some reason, tap events were still being detected, even when an image view's frame was outside of the inner parent view. That's why the problem was never detected earlier - it seemed to scroll and respond to clicks properly.
But UIContextMenuInteraction
seems to be stricter (as it should be!) and wasn't working on an image view when its frame was outside of its parent's frame.