How to dynamically adjust height of UITextView

So I know there are a couple of post like mine, but not quite what I am looking for. So here is what I am trying to accomplish. I have this ViewController that looks like this.

enter image description here

When a user inputs text into the UITextView, which currently has the text "Description", I want to dynamically change the height of the UITextView so if the text where to become multiple lines or go off screen the user can scroll through the whole view with its contents. Here is my program.

@IBOutlet var scrollView: UIScrollView!
var projectNameTextField: UITextField!
var projectCategoryLabel: UIButton!
var dueDateLabel: UIButton!
var projectDescription: UITextView!
var projectDescriptionFrame: CGRect!

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let scrollViewWidth = scrollView.frame.width
    scrollView.contentSize.width = scrollViewWidth

    let trallingSpace: CGFloat = 12.0
    let contentWidth = scrollViewWidth - (trallingSpace * 2.0)

    projectNameTextField = UITextField(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
    projectNameTextField.placeholder = "Title"
    updateScrollView(withView: projectNameTextField)


    let categoryLabel = UILabel()
    categoryLabel.text = "Category"

    projectCategoryLabel = UIButton()
    projectCategoryLabel.setTitle("Art", forState: .Normal)
    projectCategoryLabel.setTitleColor(UIColor.blackColor(), forState: .Normal)

    let stackViewHCategory = UIStackView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
    stackViewHCategory.axis = .Horizontal
    stackViewHCategory.spacing = 8
    stackViewHCategory.addArrangedSubview(categoryLabel)
    stackViewHCategory.addArrangedSubview(projectCategoryLabel)
    updateScrollView(withView: stackViewHCategory)


    let dueDateTitle = UILabel()
    dueDateTitle.text = "Due Date"

    dueDateLabel = UIButton()
    dueDateLabel.setTitle("No Due Date", forState: .Normal)
    dueDateLabel.setTitleColor(UIColor.blackColor(), forState: .Normal)

    let stackViewHDueDate = UIStackView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
    stackViewHDueDate.axis = .Horizontal
    stackViewHDueDate.spacing = 8
    stackViewHDueDate.addArrangedSubview(dueDateTitle)
    stackViewHDueDate.addArrangedSubview(dueDateLabel)
    updateScrollView(withView: stackViewHDueDate)


    projectDescription = UITextView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
    projectDescription.text = "Description"

    projectDescription.delegate = self

    projectDescription.scrollEnabled = false
    projectDescription.font = projectDescription.font?.fontWithSize(14)

    updateScrollView(withView: projectDescription)

    projectDescriptionFrame = projectDescription.frame
    projectDescriptionFrame.size.height = projectDescription.contentSize.height
    projectDescription.frame = projectDescriptionFrame
}

func textViewDidChange(textView: UITextView) {

    projectDescriptionFrame.size.height = projectDescription.contentSize.height
    projectDescription.frame = projectDescriptionFrame

    var contentSizeheight = CGFloat()

    for (index, viewInScrollView) in scrollView.subviews.enumerate() {
        if index > 1 {
            contentSizeheight += viewInScrollView.frame.height
        }
    }

    scrollView.contentSize.height = contentSizeheight
}


func updateScrollView(withView withView: UIView) {
    scrollView.addSubview(withView)
    scrollView.contentSize.height += withView.frame.height
}

func scrollViewContentHeight() -> CGFloat {

    var height = CGFloat()

    for (index, viewInScrollView) in scrollView.subviews.enumerate() {
        if index > 1 {
            height += viewInScrollView.frame.height
        }
    }

    return height
}

How can I do this so that it will adjust the UITextView frame and update the UIScrollView.contentSize (Not of UITextView) so then the user can scroll through the whole view. And I would say I want a UI like when you make a new message in iOS Mail app. There you are able to scroll through the emails subject and the body of the message as one. Please help me with this. I have been stuck on this for days.

enter image description here


ANSWERS:


Okay I figured it out. Here is what I changed.

In the global properties.

var oldProjectDescriptionHeight: CGFloat!

var projectDescriptionHeight: CGFloat! {
    willSet {
       oldProjectDescriptionHeight = projectDescriptionHeight
    }
}

And in the UITextViewDelegate textViewDidChange

func textViewDidChange(textView: UITextView) {

    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    var newFrame = textView.frame
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    textView.frame = newFrame

    projectDescriptionHeight = newFrame.height

    scrollView.contentSize.height += projectDescriptionHeight - oldProjectDescriptionHeight
}


 MORE:


 ? Get number of lines in UITextView without contentSize.height
 ? Get number of lines in UITextView without contentSize.height
 ? Get number of lines in UITextView without contentSize.height
 ? Background Image for UITextView
 ? iOS7 UITextView contentsize.height alternative
 ? Get number of lines UITextView without using "\n"?
 ? UITextView does not update its height after trying to change its height programmatically
 ? UITextView Font Size
 ? How to limit the number of lines in UITextView?
 ? Wrapping text in UITextView in Swift