我有一个scrollView,它由detailView 中的3 个textView、按钮和标签组成。我使用 3 个文本视图,因为我的视图标题、日期和描述需要不同的字体。问题是有时描述很长,有时又很小,而且与标题相同。那么视图看起来根本就不好,因为标题和描述之间有很多空白。是否可以动态设置textView和滚动视图的大小,或者是否有更好的方法来解决这个问题。提前感谢
您需要调整文本视图的框架以匹配其各自的内容大小(请参阅此处的最佳答案:如何根据其内容调整 UITextView 的大小? https://stackoverflow.com/questions/50467/how-do-i-size-a-uitextview-to-its-content关于如何做到这一点,使用textview的contentSize属性),然后根据所有控件的框架调整scrollView的contentSize。
假设您的文本视图垂直连续放置(如果有间距等,只需调整代码):
// (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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)