Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a UITextView the focus in tvOS?

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];
            }
        }

    }
}
like image 951
user3160965 Avatar asked Nov 02 '15 17:11

user3160965


1 Answers

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.

like image 94
Tom Harrington Avatar answered Nov 15 '22 08:11

Tom Harrington