简体   繁体   English

根据内容自动增加视图的高度

[英]Automatically Grow a View's Height Based on It's Content

Situation 情况

I have a (vertical) UIStackView containing both a plain UIView of height 50 (named sliderView ) and a UILabel of height 36 defined in my storyboard. 我有一个(垂直) UIStackView包含一个高度为50的普通UIView (称为sliderView ),又包含一个在故事板中定义的高度为36UILabel The label's alpha property is initially set to 0.0 to make it invisible. 标签的alpha属性最初设置为0.0 ,以使其不可见。

In the controller's viewDidLoad I use UIViewController Containment to add another view controller's view to as a subview of sliderView . 在控制器的viewDidLoad我使用UIViewController Containment将另一个视图控制器的视图添加为sliderView的子视图。 This new subview does not necessarily match sliderViews height. 此新子视图不一定与sliderViews高度匹配。 It might actually a fair bit taller. 实际上可能更高。

At first, this setup looks fine. 首先,此设置看起来不错。 Once I make the label visible, I see that it still starts at a y-position of 50 . 使标签可见后,我看到它仍然从50的y位置开始。 So, the sliderView did not automatically stretch to use it's new child's height. 因此, sliderView不会自动拉伸以使用其新孩子的身高。 Makes sense. 说得通。

Question

I thought that I could easily just call sizeToFit on sliderView to make those two heights fit. 我以为我可以轻松地在sizeToFit上调用sliderView来使这两个高度合适。 Unfortunately, this does not seem to work. 不幸的是,这似乎不起作用。 Am I misunderstanding something here? 我在这里误会什么吗? Thanks! 谢谢!

Use following method to get the CGSize required for NSString text. 使用以下方法获取NSString文本所需的CGSize

- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
    CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
    size = CGSizeMake(frame.size.width, frame.size.height+20.0f);
}
return size;
}

Here width parameter is the width of your label and font is the font specified for your label . 在这里, width参数是label的宽度,而font是为label指定的字体。 Call CGSize.height to get the height. 调用CGSize.height获取高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据内容动画 UIScrollView 的高度 - Animate the height of a UIScrollView based on it's content 弹出窗口内容视图的额外高度 - Extra height of popover's content view 将表格视图单元格扩展到内容的高度 - Expand table view cell to the content's height 根据视图的高度添加约束iOS - Add constraints based on view's height iOS 如何根据内容动态调整UICollectionViewCell的高度? - How do I dynamically size UICollectionViewCell's height based on content? UITableViewCell的内容视图高度未针对动态字体(NSLayoutConstraints)进行调整 - UITableViewCell's content view height is not adjusting for the dynamic font (NSLayoutConstraints) 约束不明确地表明表格视图单元格的内容视图的高度为零 - Constraints ambiguously suggest a height of zero for tableview cell's content view 如何根据背景颜色自动更改视图的透明颜色 - How to automatically change transparent color of a view based on the color of it’s background iOS,scrollView内容大小的高度等于内容视图的高度,但它仍然可以垂直滚动? - iOS, the height of scrollView content size is equal to the content view's height, but it can still scroll vertically? 如何使用Autolayout动态基于子视图的可用性调整视图高度的大小? - How to resize view height based on it's subview avalability dynamically with Autolayout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM