UITextView and other elements inside UIScrollView with AutoLayout

58 views Asked by At

I have my view in the following way.

enter image description here

I am expanding the text view programmatically depending on the text as follows

func textViewDidChange(_ textView: UITextView) {
    let size = CGSize(width: titleAndDescriptionContainerView.frame.width, height: .infinity)
    let estimatedSize = noteDescription.sizeThatFits(size)
    noteDescription.constraints.forEach{
        (constraint) in if constraint.firstAttribute == .height{
            constraint.constant = estimatedSize.height
        }
    }
    titleAndDescriptionContainerView.constraints.forEach{
        (constraint) in if constraint.firstAttribute == .height{
            constraint.constant = estimatedSize.height + noteTitle.frame.height
        }
    }
    entireScrollView.contentSize = CGSize(width: entireScrollView.frame.width, height: (imageScrollView.frame.height + titleAndDescriptionContainerView.frame.height + audioContainerView.frame.height))
}

I am also changing the size of the entire scroll view. But I am not able to scroll my view when the text is long. have a look at these screenshots.

enter image description here


enter image description here

1

There are 1 answers

0
rs7 On

Your constraints are not correctly defined. You need to redefine them as follows:

  1. Make sure your EntireScrollView is pinned to the edges of the root view.

  2. Pin to the EntireScrollView Content Layout Guide the following:

    • ImageScrollView (top, leading, trailing)
    • Audio Container View (leading, trailing)
    • Title And Description Container view (leading, trailing, bottom)
  3. ImageScrollView, Audio Container View, and Title And Description Container View need to be of equal width with the EntireScrollView Frame Layout Guide.