uikituiscrollviewuicontextmenuinteraction

UIContextMenuInteraction not working if view is originally offscreen


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?


Solution

  • In 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.