[英]How can I make a UITextView the focus in tvOS?
在tvOS中,有一個集合視圖。 每個“行”是一個圖像。 每個圖像都有與之關聯的文本。 當用戶“懸停”在圖像上時,文本將更改為與圖像匹配。 但是,我希望用戶能夠轉到文本而不將焦點放在所選圖像和UITextView之間的圖像之間。
使用播放/暫停按鈕,我可以切換UITextView的背景顏色,但UITextView無法獲得焦點。 用戶必須使用遙控器來選擇UTTextView。
這是代碼:
- (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];
}
}
}
}
默認情況下, UITextView
無法專注於tvOS,這意味着它從canBecomeFocused
返回NO
。 如果您可以選擇文本視圖,那么它是可聚焦的。 現在有一個錯誤,在界面構建器中檢查“可選”不起作用,但如果你在代碼中將selectable
設置為YES
,那么它可以工作。
但請注意, UITextView
在獲得焦點時不會提供任何UI差異。 如果您想要任何焦點的視覺指示,您必須自己更改文本視圖的外觀。
另請注意, preferredFocusedView
是一個只讀屬性,因此除非使用結果,否則調用該方法無效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.