【问题标题】:UITextview does not scroll to topUITextview 不滚动到顶部
【发布时间】:2016-08-11 19:23:15
【问题描述】:

我正在使用 UITextView 在 ViewDidLoad() 中显示长文本:

var story1 = "Researchers on Friday introduced the stunningly realistic-looking interactive humanoid robot to the world, and she reacted by nodding her head, squinting, moving her hand and eyeballs and telling assembled local photographers, with her speech synched to her lip movements, to snap her face from a more flattering angle. Girl's got spirit. \n\nJia Jia made her public debut at the University of Science and Technology of China, where she came to life over the course of three years. While her rosy cheeks and pink lips make her look like she has blood running through her actuators, she still has a way to go before she'll be confused with a human. Her expressions are currently limited to 'microexpressions' and her speech has that distinct stiffness that screams 'I am a robot and I will one day chase you from your home'\n\......." // a very long text with many rows
self.storyTextView.text = story1

如果我为storyTextView 设置Scrolling Enabled,则textview 不会从头开始显示文本。我可以在文本的中间(或底部)看到滚动指示器。

见截图1。

我试过了

self.storyTextView.scrollsToTop = true

但这无济于事。

如果我取消选中Scrolling Enabled,或者如果文本不是太长而无法在 UITextView 中显示整个文本,它将按预期从头开始显示。见截图2。

有没有办法从文本的开头显示长文本(将滚动指示器移动到顶部)?我检查了约束和布局,但似乎不相关。我确实搜索了很多,但直到现在还没有运气。我可能错过了一些简单的设置,但请任何人帮助我。

【问题讨论】:

    标签: swift text scroll uitextview display


    【解决方案1】:

    在 ViewController 中添加以下方法

     override public func viewWillLayoutSubviews() {
       super.viewWillLayoutSubviews()
       textView.contentOffset = CGPoint.zero
     }
    

    【讨论】:

      【解决方案2】:

      在我的情况下,这个解决方案是可行的,Swift 4:

      override func viewDidLayoutSubviews() {
          self.txt_about.setContentOffset(CGPoint.zero, animated: false)
      }
      

      【讨论】:

        【解决方案3】:

        适用于 iOS 11 和 swift 4 的唯一解决方法是 在 viewDidLoad 处设置滚动关闭并在 viewDidAppear 处设置为 ON。 它适用于 UIScrollView 和 UiTextView。 测试代码。

         override func viewDidLoad() {
            super.viewDidLoad()
            scrollView.isScrollEnabled = false
         }
        
         override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            scrollView.isScrollEnabled = true
         }
        

        【讨论】:

          【解决方案4】:

          试试这个: self.storyTextView.scrollRangeToVisible(NSRange(location:0, length:0))

          【讨论】:

          • 是什么让你觉得他还没有在主线程上做呢?
          【解决方案5】:

          使用contentOffset属性:

          textView.contentOffset = CGPointZero
          

          Swift 3 更新:

          textView.contentOffset = CGPoint.zero
          

          【讨论】:

          • 谢谢。你是对的。而且我从另一个问题的答案中知道,这需要在 viewDidLayoutSubviews() 中完成。 stackoverflow.com/questions/28053140/…
          • 我添加了这个,问题就解决了。 override func viewDidLayoutSubviews() { self.StoryTextView.setContentOffset(CGPointZero, animated: false) }
          • Swift 3: CGPointZero -> CGPoint.zero
          猜你喜欢
          • 2015-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多