简体   繁体   中英

Swift : View in a UIScrollView is unable to detect touch

So I have A UIButton in a UIView within a UIScrollView within a UIViewController within a UIScrollView .. that is whole lot of layers I know but that was required. But the result I got is that the UIButton cannot be tapped because the touch can't be detected on the button. ScrollViews are working perfectly but the elements in the final layer are not detecting touches.

The Layers are as follow:

UIViewController
~UIScrollView
~~UIViewController
~~~UIScrollView
~~~~UIView
~~~~~UIButton

Kindly tell me how can I detect the touch on the last UIView?

you should debug view hierarchy by clicking debug view hierarchy button

在此处输入图片说明 look like this.

So you come to know that your button actually get touch events or it is hidden by other views.

Update :

I am uploading another screenshot

在此处输入图片说明

You have to run your project then you will able to see this button above console pane.

You can refer Apple documentation for more details.

Hope this will help :)

  1. To solve your current problem, you need to iterate through all gestures on each super view and call requireGestureRecognizerToFail for relevant gestures on subviews. This way you make sure that the subviews get the touches 'first' and superviews handle only those touches which are ignored or not recognize by subviews for whatever reason. (I know this sounds as ridiculous as having a view hierarchy like UIViewController -> UIScrollView -> UIViewController -> UIScrollView -> UIView -> UIButton )

  2. It seems unlikely that you could not have solved your visual design using either UITableView or UICollectionView . So reconsider!! It will save you a lot of such issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM