Nathan Schafer
Nathan Schafer

Reputation: 273

Scroll Position of UITextView at the top

I am using swift and want a UITextView to be at the top when the view launches. At the moment when I launch the app the UITextView is scrolled to the end. I have tried looking online and think scrollRangeToVisible might work but do not know how to use it in swift.

import UIKit

class ThirdViewController: UIViewController {

    @IBOutlet weak var FunFact: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        FunFact.scrollRangeToVisible(0, 0)
        // Do any additional setup after loading the view.
    }
}

Upvotes: 2

Views: 1863

Answers (2)

user2966769
user2966769

Reputation:

Try this:

var zeroOffset = CGPoint.zeroPoint
FunFact.contentOffset(zeroOffset)

This should bring the offset to 0 (the offset is an indication of how far the current position is from the initial one)

Upvotes: 0

Torsten B
Torsten B

Reputation: 412

Add this to your ViewController:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        textView.layoutIfNeeded()
        textView.contentOffset = CGPoint.zero
    }

This scrolls the UITextView to the top. Since it only knows its size, when it is layouted, "layoutIfNeeded" is needed after you changed the text (i changed it in viewDidLoad).

Upvotes: 2

Related Questions