最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

uikit - UIContextMenuInteraction not working if view is originally offscreen - Stack Overflow

programmeradmin2浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论