7

I have a UITextView in my TV app, when i try to make it focusable then the UI is not making it focusable and i am unable to scroll it. I read some questions about it and some says that its a known issue and we should use storyboards. Actually i am using storyboards but still unable to get this to work. I have also tried to make it selectable, scrollEnabled and userInteractionEnabled and then finally also tried this line of code but none of them is working.

descriptionLbl.panGestureRecognizer.allowedTouchTypes = [NSNumber(integer: UITouchType.Direct.rawValue)]

I also tried to print bounds abd contentSize of the UITextView and here is the log

Content Size (740.0, 914.0)
Bounds (25.0, 0.0, 740.0, 561.0)

Here is my code can some body help me

@IBOutlet weak var descriptionLbl: UITextView!
var currentModel : Model?


override func viewDidLoad() {
    super.viewDidLoad()

    descriptionLbl.selectable = true
    descriptionLbl.scrollEnabled = true
    descriptionLbl.userInteractionEnabled = true


    descriptionLbl.panGestureRecognizer.allowedTouchTypes = [NSNumber(integer: UITouchType.Direct.rawValue)]


    descriptionLbl.text = (currentModel?.description)! + (currentModel?.description)! + (currentModel?.description)!
    descriptionLbl?.contentInset = UIEdgeInsets(top: 0.0, left: -25.0, bottom: 0.0, right: 0.0);


}

i guess i should not be doing these many tricks but it is not working in any way. The focus is actually coming to UITextView but it does not scroll. Any ideas?

1
  • (2023) There seems to be a very odd bug/behavior that it Just Don't Work if you set them on storyboard, gotta set in code. Wild.
    – Fattie
    Commented Oct 9, 2023 at 13:30

5 Answers 5

11

I believe you want Indirect, not Direct touches. Here is how I set up my focusable, scrollable UITextView:

self.selectable = YES;
self.userInteractionEnabled = YES;
self.panGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeIndirect)];
1
  • by making it userInteractionEnabled, it's automatically focusable Commented Nov 9, 2015 at 15:55
9

For Swift 5, this is what worked for me:

    textView.isUserInteractionEnabled = true;
    textView.isScrollEnabled = true;
    textView.showsVerticalScrollIndicator = true;
    textView.bounces = true;
    textView.panGestureRecognizer.allowedTouchTypes = [NSNumber(value: UITouch.TouchType.indirect.rawValue)]
3
  • 1
    You just need to use: view.isUserInteractionEnabled = true and view.panGestureRecognizer.allowedTouchTypes = [NSNumber(value: UITouch.TouchType.indirect.rawValue)]
    – Diogo T
    Commented Jun 12, 2020 at 20:15
  • (2023) There seems to be a very odd bug/behavior that it Just Don't Work if you set them on storyboard, gotta set in code. Wild.
    – Fattie
    Commented Oct 9, 2023 at 13:30
  • @DiogoT very surprisingly it DID NOT work for me unless I used, wait for it, the bounces clause. WTH.
    – Fattie
    Commented Oct 9, 2023 at 13:30
7

Here is what worked for me, in Swift 2.1:

myTextView.userInteractionEnabled = true
myTextView.selectable = true
myTextView.scrollEnabled = true
myTextView.panGestureRecognizer.allowedTouchTypes = [UITouchType.Indirect.rawValue]

Hope it helps.

1
  • scrollEnabled seems to be true by default, so maybe redundant
    – Andy
    Commented Mar 14, 2016 at 12:21
6

Here's what worked for me in Swift 3.0.1, based on the previous answers:

  • I had to subclass UITextView to override canBecomeFocused to return true
  • I had to set panGestureRecognizer.allowedTouchTypes = [NSNumber(value: UITouchType.indirect.rawValue)]
  • I had to set isUserInteractionEnabled = true in code - setting it in Interface Builder didn't seem sufficient
  • I had to check "Scrolling Enabled" in Interface builder (setting isScrollEnabled = true in code would also work

A few other niceties I found:

  • bounces = true made scrolling feel more natural (had to be set in code; IB setting not respected)
  • showsVerticalScrollIndicator = true had to be set in code; IB setting not respected
  • Overriding didUpdateFocus(in:with:) to change the background color to visually indicate the focus.
3
  • "Cannot override mutable property with read-only property 'isScrollEnabled'"
    – Abhishek
    Commented Mar 31, 2017 at 6:15
  • It is giving the above error while setting the property isScrollEnabled in latest swift 3 and xcode versions.
    – Abhishek
    Commented Mar 31, 2017 at 6:16
  • you dont need to use "canBecomeFocused to return true" but still this is a big help
    – reggie
    Commented Jan 9, 2018 at 6:33
0

If you use objective-C to do this, It would be very sample:

    _textView.panGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeIndirect)];
    _textView.selectable = YES;
    _textView.scrollEnabled = YES;
    _textView.userInteractionEnabled = YES;

Above these are enough, However, If you use swift to do this, It doesn't work.

1
  • 1
    In swift, you can set: textView.isUserInteractionEnabled = true textView.isScrollEnabled = true textView.panGestureRecognizer.allowedTouchTypes = [NSNumber(value: UITouch.TouchType.indirect.rawValue)] Commented Apr 28, 2019 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.