2015-11-06 76 views
5

我在我的電視應用程序中有一個UITextView,當我嘗試使它可以焦點時,然後用戶界面沒有使其可聚焦,我無法滾動它。我讀了一些關於它的問題,有人說它是一個已知的問題,我們應該使用故事板。其實我正在使用故事板,但仍然無法得到這個工作。我也試圖使它selectable,scrollEnableduserInteractionEnabled然後最後也嘗試了這一行代碼,但他們都沒有工作。UITextView不滾動在tvOS

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

我也試圖打印bounds ABD中UITextViewcontentSize這裏是日誌

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

這裏是我的代碼可一些身體幫我

@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); 


} 

我想我應該不要做這些許多技巧,但它不以任何方式工作。重點實際上是UITextView,但它不滾動。有任何想法嗎?

回答

7

我相信你想要間接,而不是直接觸摸。這裏是我建立了我的可調焦,可滾動的UITextView:

self.selectable = YES; 
self.userInteractionEnabled = YES; 
self.panGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeIndirect)]; 
+0

它是如何聚焦的? – user3160965

+0

通過使其userInteractionEnabled,它可以自動聚焦 –

4

這裏是爲我工作,在雨燕2.1:

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

希望它能幫助。

+0

'scrollEnabled'似乎是默認情況下,所以也許多餘 – Andrei

2

下面是我在斯威夫特3.0.1什麼工作的基礎上,以前的答案:

  • 我不得不繼承UITextView覆蓋canBecomeFocused返回true
  • 我必須設置panGestureRecognizer.allowedTouchTypes = [NSNumber(value: UITouchType.indirect.rawValue)]
  • 我必須在代碼中設置isUserInteractionEnabled = true - 將其設置在Interface Builder中似乎不夠用
  • 我必須在界面構建器中檢查「滾動已啓用」(設置isScrollEnabled = true i n個代碼也將工作

其他一些細微,我發現:

  • bounces = true做出滾動的手感更自然(必須在代碼中設置; IB設置不受尊重)
  • showsVerticalScrollIndicator = true必須在代碼中設置; IB設置不受尊重
  • 覆蓋didUpdateFocus(in:with:)更改背景顏色以直觀指示焦點。
+0

「無法覆蓋mutable屬性只讀屬性isScrollEnabled'」 – abhishek

+0

它給出了上述錯誤,同時設置屬性isScrollEnabled在最新的swift 3和xcode版本中。 – abhishek

+0

你不需要使用「canBecomeFocused返回true」,但這仍然是一個很大的幫助 – reggie