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

后端 未结 2 869
孤城傲影
孤城傲影 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;
    
    0 讨论(0)
  • 2021-01-27 17:50

    You could set the size of the frame to be dependent on the character length by setting the frame at the onset to be:

    CGRectMake (0.0, 0.0, [yourString count]*10, 30.0);
    

    This is what I did when I had a UIPopover come up with a variable name.

    0 讨论(0)
提交回复
热议问题