How can i fix my scrollView dynamically when i have more than one textViews in it?

后端 未结 2 868
孤城傲影
孤城傲影 2021-01-27 16:56

I have a scrollView which consists of 3 textViews, buttons and labels in a detailView. i am using 3 text view because i need different fonts for my view title, date and descript

2条回答
  •  隐瞒了意图╮
    2021-01-27 17:48

    You need to adjust the frames of the text views to match their respective contentSizes (see the top answer here: How do I size a UITextView to its content? on how to do that, uses the contentSize property of the textview), then you adjust the contentSize of the scrollView based on the frames of all the controls.

    Assuming your textviews are placed consecutively vertically (just adjust the code if there is spacing, etc.):

    // (first adjust each text view's frame per the linked answer)
    ...
    // then adjust the frames of the content
    CGRect topTextViewFrame = topTextView.frame;
    CGRect middleTextViewFrame = middleTextView.frame;
    middleTextViewFrame.origin.y = topTextViewFrame.origin.y + topTextViewFrame.size.height;
    middleTextView.frame = middleTextViewFrame;
    CGRect bottomTextViewFrame = bottomTextView.frame;
    bottomTextViewFrame.origin.y = middleTextViewFrame.origin.y + middleTextViewFrame.size.height;
    // then adjust your other controls based on these frames, for example:
    CGRect myButtonFrame = myButton.frame;
    myButtonFrame.origin.y = bottomTextViewFrame.origin.y + bottomTextViewFrame.size.height;
    // finally adjust the contentSize of the scrollview assuming the button is the bottom element
    CGSize csize = myScrollView.contentSize;
    csize.height = myButtonFrame.origin.y + myButtonFrame.size.height;
    myScrollView.contentSize = csize;
    

提交回复
热议问题