How can I make a UITextView the focus in tvOS?
Asked Answered
E

1

5

In tvOS, there is a collection view. Each "row" is an image. Each image has text associated with it. When the user "hovers" over an image, the text changes to match the image. But, I want the user to be able to go to the text without giving the focus to the images between the one "chosen" and there others that are between the chosen image and the UITextView.

Using the Play/Pause button, I am able to toggle the background color of the UITextView, but the UITextView does not get the focus. The user has to use the remote to choose the UTTextView.

Here is the code:

- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIEvent *)event {

    for (UIPress *item in presses)
    {
        if(item.type == UIPressTypePlayPause) {

            //should reactivate uicollectionview

            if (_isPlay) {
                _isPlay = NO;
                _artistsCollectionView.userInteractionEnabled = YES;
                _textViewForBiosAndHelp.backgroundColor = [UIColor lightGrayColor];
                [_textViewForBiosAndHelp resignFirstResponder];
                [_artistsCollectionView becomeFirstResponder];
                [_artistsCollectionView preferredFocusedView];


               //the purpsoe is to go to the textview. it does not.
            }else{
                _isPlay = YES;
                 _artistsCollectionView.userInteractionEnabled = NO;
                _textViewForBiosAndHelp.backgroundColor = _aColor;
                [_artistsCollectionView resignFirstResponder];
                [_textViewForBiosAndHelp becomeFirstResponder];
                [ _textViewForBiosAndHelp preferredFocusedView];
            }
        }

    }
}
Expansible answered 2/11, 2015 at 17:21 Comment(0)
J
7

By default UITextView is not focusable on tvOS, meaning that it returns NO from canBecomeFocused. If you make the text view selectable, it's focusable. Right now there's a bug where checking "selectable" in interface builder doesn't work, but if you set selectable to YES in code then it works.

But note that UITextView doesn't provide any UI differences when it gets focus. If you want any visual indication of focus, you'll have to change the text view's appearance yourself.

Also note that preferredFocusedView is a read-only property, so calling that method has no effect unless you use the result.

Jawbone answered 6/11, 2015 at 21:27 Comment(1)
Let me rephrase. What I need to do is this: Use the Play/Pause button to make the UITextView focused and "defocus" the row that has the "Adjusts Image When Focused" checked in IB making the image not adjusted. Then have the reverse happen when the Play/Pause button is tapped again.Expansible

© 2022 - 2024 — McMap. All rights reserved.