UITextView will not scroll to bottom under specific circumstances

Within my UIViewController I have a UITextView which I use to dump current status and info into. Obviously evry time I add text to the UITextView I would like it to scroll to the bottom.

So I've created this function, which I call from UIViewController whenever I have new data.

func updateStat(status: String, tView: UITextView) {
	db.status = db.status + status + "\n"
	tView.text = db.status
	let range = NSMakeRange(tView.text.count - 1, 0)
	tView.scrollRangeToVisible(range)
	tView.flashScrollIndicators()
}

The only thing that does not work is the tView.scrollRangeToVisible. However, if from UIViewController I call:

updateStat(status: "...new data...", tView: mySession)
let range = NSMakeRange(mySession.text.count - 1, 0)
mySession.scrollRangeToVisible(range)

then the UITextView's scrollRangeToVisible does work.

I'm curious if anyone knows why this works when called within the UIViewController, but not when called from a function?

p.s. I have also tried the updateStatus function as an extension to UIViewController, but that doesn't work either

Replies

Could you print(range) in both cases and tell what you get ?

In the second code snippet, did you change updateStat (removing the range and scrollRange from within the func) ?