Determine if UIView is visible to the user?

前端 未结 12 1787
不知归路
不知归路 2020-12-07 15:21

is it possible to determine whether my UIView is visible to the user or not?

My View is added as subview several times into a Tab Bar

12条回答
  •  离开以前
    2020-12-07 16:12

    I you truly want to know if a view is visible to the user you would have to take into account the following:

    • Is the view's window not nil and equal to the top most window
    • Is the view, and all of its superviews alpha >= 0.01 (threshold value also used by UIKit to determine whether it should handle touches) and not hidden
    • Is the z-index (stacking value) of the view higher than other views in the same hierarchy.
    • Even if the z-index is lower, it can be visible if other views on top have a transparent background color, alpha 0 or are hidden.

    Especially the transparent background color of views in front may pose a problem to check programmatically. The only way to be truly sure is to make a programmatic snapshot of the view to check and diff it within its frame with the snapshot of the entire screen. This won't work however for views that are not distinctive enough (e.g. fully white).

    For inspiration see the method isViewVisible in the iOS Calabash-server project

提交回复
热议问题