UITextView scrolling without focus

In the DescriptiveAlert sample from the TVMLCatalog, the text view is scrolled with up and down swipes, without being focused, because the focus is always on either buttons (changed with left/right)


I haven't been able to replicate the same behaviour, the only way I can make the text view scroll is to give it focus, which leads to a different behaviour (addotional swipes, plus need to scroll to bottom to move to the buttons)


Using the View Degubber I can see that the sample is using a standard UITextView and not some private class, but is that behaviour private? Do I need to manually handle the scrolling myself?


(As a side question, in that sample the text view also fades the top and bottom when scrolling, is that also a private thing?)


Thanks

There's a trick to it, using the gesture recognizers exposed on the UITextView.


Your text view has two gesture recognizers, the panGestureRecognizer and the directionalPressGestureRecognizer, which are inherited from UIScrollView. A little-known trick with those gesture recognizers is that you can actually add them to a view other than the scroll view that they're attached to, which is what you want to do here. Whatever view that contains both your text view and your buttons is the best view to use for this:


[myParentView addGestureRecognizer:myTextView.panGestureRecognizer];

[myParentView addGestureRecognizer:myTextView.directionalPressGestureRecognizer];


Then, you need to enable the directionalPressGestureRecognizer (it's disabled by default) and add UITouchTypeIndirect to the panGestureRecognizer's allowedTouchTypes, and you should be good to go.


You won't need to make the text view focusable: only your buttons will need to accept focus.


The reason this works is because the touch and press events are going to the buttons, which are siblings of the text view and so the events don't go up the responder chain to the text view. But by moving those gestures to a view that is in the responder chain above the buttons, the text view can receive those events.

Thanks, I was almost there, I either moved the gesture recognizer or set the allowedTouchTypes, but not both at the same time.


But there's a problem with directionalPressGestureRecognizer, which is not public at the moment. Is that coming in a future build?

I am experiencing the same problem. Is there a solution for this?

In my view controller i have textview and a button. Button is placed below textview.

Following the approach provided above, the focus is on button but the textview is not scrolling.


Following is the code implemented:


self.hardWallTextView.panGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeIndirect)];

[self.view addGestureRecognizer:self.hardWallTextView.panGestureRecognizer];


Am I missing something?

That sounds right, I don't think you're missing anything. Might there be some other view in the hierarchy that's getting the touch instead of self.view?

The problem is coming with Custom Button. If I use default button, the textview is scrollable while the focus is on button.


Is there is any way the same functionality can be achived by using the custom button?

UITextView scrolling without focus
 
 
Q