UITextView that expands to text using auto layout

前端 未结 16 2051
面向向阳花
面向向阳花 2020-11-28 01:25

I have a view that is laid out completely using auto layout programmatically. I have a UITextView in the middle of the view with items above and below it. Everything works f

16条回答
  •  眼角桃花
    2020-11-28 01:52

    This more of a very important comment

    Key to understanding why vitaminwater's answer works are three things:

    1. Know that UITextView is a subclass of UIScrollView class
    2. Understand how ScrollView works and how its contentSize is calculated. For more see this here answer and its various solutions and comments.
    3. Understand what contentSize is and how its calculated. See here and here. It might also help that setting contentOffset is likely nothing but:

    func setContentOffset(offset: CGPoint)
    {
        CGRect bounds = self.bounds
        bounds.origin = offset
        self.bounds = bounds
    }
    

    For more see objc scrollview and understanding scrollview


    Combining the three together you'd easily understand that you need allow the the textView's intrinsic contentSize to work along AutoLayout constraints of the textView to drive the logic. It's almost as if you're textView is functioning like a UILabel

    To make that happen you need to disable scrolling which basically means the scrollView's size, the contentSize's size and in case of adding a containerView, then the containerView's size would all be the same. When they're the same you have NO scrolling. And you'd have 0 contentOffset. Having 0 contentOffSet means you've not scrolled down. Not even a 1 point down! As a result the textView will be all stretched out.

    It's also worth nothing that 0 contentOffset means that the scrollView's bounds and frame are identical. If you scroll down 5 points then your contentOffset would be 5, while your scrollView.bounds.origin.y - scrollView.frame.origin.y would be equal to 5

提交回复
热议问题