I have a UIScrollView with multiple views inside of it, one of them being a UITextView. I'm trying to set the height of scroll view to match the content. The problem is that the UITextView has a height that varies according to the text it contains. This content is set in the override viewDidLoad() method in the view controller, but for some reason the value I get for the height of the view does not reflect any changes, even after the value of the content has changed. I have a function written to change the height of the content subview by changing it's height constraint constant, which I am calling in the viewDidAppear() method. Relevant code is as follows:
func setPageSize() {
var pageHeight : CGFloat {
var height : CGFloat = 1000 // The height of all content besides the UITextView
let descriptionTextViewHeight = self.descriptionTextView.frame.height
height += descriptionTextViewHeight
return height // Always returns 60, which is the height of view when using the placeholder text from interface builder (doesn't update when text updated).
}
self.pageViewHeightConstraint.constant = pageHeight
}
override func viewDidLoad() {
super.viewDidLoad()
self.descriptionTextView.text = self.description
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
self.setPageSize()
}
Should I call the setPageSize() method somewhere else? Is there a method in UITextView that returns an updated value when new text is entered? I have also tried using the location of the last view in the content view for reference in setting the height, but I'm having the same problem - it's returning a value as if the UITextView's height is always 60, which it's not.