UITextView is not scrolled to top when loaded

后端 未结 12 1236
别跟我提以往
别跟我提以往 2020-12-03 02:39

When I have text that does not fill the UITextView, it is scrolled to the top working as intended. When there is more text than will fit on screen, the UITextView is scrolle

12条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 03:11

    This is an interesting bug. In our project, this is only occurring on devices with an iPhone 5-size screen. It appears that the textview contentOffset changes at some point during the view controller lifecycle. In viewDidLoad and viewWillAppear the textview's contentOffset is 0,0, and by viewDidAppear it's changed. You can see it happening in viewWillLayoutSubviews. Constraints appear to be set up correctly.

    This will ensure you don't call a scrolling method unless it's needed:

    if textView.contentOffset.y > 0 {
        textView.contentOffset = CGPoint(x: 0, y: 0)
        // Or use scrollRectToVisible, scrollRangeToVisible, etc.
    }
    

提交回复
热议问题