UITextView is not scrolled to top when loaded

后端 未结 12 1291
别跟我提以往
别跟我提以往 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:13

    For me this works in a different way, I tried all things mentioned above but none of the worked in func viewWillAppear(_ animated: Bool). Which eventually makes textView scrolled up, and in func viewDidAppear(_ animated: Bool) it would scroll after screen appeared.

    Below worked for me but got some constraint related issue with keyboard up and down.

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

    Below worked as expectation:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.textView.scrollsToTop = true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.layoutIfNeeded()
        self.textView.setContentOffset(.zero, animated: false)
    }
    

提交回复
热议问题