带有自动布局的 UIScrollView 内的动态 UILabel 高度

时间:2022-11-03
本文介绍了带有自动布局的 UIScrollView 内的动态 UILabel 高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用带有 UIScrollView 的自动布局来显示对象的一些属性.我从网络服务动态下载此信息.滚动视图具有恒定宽度(因为我不希望有垂直滚动行为),并且它的子视图通过一组约束尊重这个宽度,但我不能增加 UILabel 的高度动态.

I am using autolayout with UIScrollView to show some attributes from an object. I download this information dinamically from web service. The scrollview has a constant width (because I don't want to have a vertical scroll behavior) and its subviews respect this width with a group of constraints, but I can't to increase the UILabel's height dynamically.

我编写所有代码并使用 viewDidLoad 选择器创建子视图...

I code everything and I use viewDidLoad selector to create subviews...

- (void)viewDidLoad {
    [super viewDidLoad];
    .
    .
    .

    UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    descriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;
    descriptionLabel.numberOfLines = 0;
    descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
    descriptionLabel.opaque = YES;
    descriptionLabel.backgroundColor = [UIColor clearColor];
    descriptionLabel.textColor = [UIColor whiteColor];
    descriptionLabel.textAlignment = NSTextAlignmentRight;
    descriptionLabel.font = [UIFont appetitoMediumItalicFontWithSize:15.0f];
    descriptionLabel.text = NSLocalizedStringFromTable(@"APT_DISH_DETAIL_DESCRIPTION", @"DishDetail", @"Etiqueta que contiene la descripción del platillo");
    [descriptionLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.detailContentScrollView addSubview:descriptionLabel];
    self.descriptionLabelS = descriptionLabel;

    .
    .
    .
}

您可以查看 self.detailContentScrollView 变量,这是从视图控制器的 nib 创建的 IBOUlet.

You can watch the self.detailContentScrollView variable, this is an IBOulet created from view controller's nib.

然后我使用 updateConstraints 选择器...

Then I use the updateConstraints selector...

- (void)updateConstraints {
[super updateConstraints];
// This dictionary has more variables, ok
NSDictionary *viewsDict = @{@"dish_description_label": self.descriptionLabelS};
.
.
.

[self.descriptionLabelS setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.detailContentScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[view1][dish_description_label]-[view2][view3][view4]-|" options:0 metrics:nil views:viewsDict]];

.
.
.
}

最后,当我收到 Web 服务的信息时,我从滚动视图发送 sizeToFitUILabel 选择器和 layoutIfNeeded.但是我的 UILabel 永远不会用新内容调整自己的大小.我做错了什么?

and finally, when I receive the web service's info, I send sizeToFit's UILabel selector and layoutIfNeeded from the scrollview. But my UILabel never resizes itself with the new content. What am I doing wrong?

推荐答案

UIScrollView 内容大小是通过自动布局动态更新的,或许你只需要做到以下几点

UIScrollView content size is updated dynamically with autolayout, maybe you only must do the following

- (void) setupScroll
{
    [_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_contentView setTranslatesAutoresizingMaskIntoConstraints:NO];

    [_scrollView addSubview:_contentView];

    NSArray *horizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_contentView]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:NSDictionaryOfVariableBindings(_contentView)];
    NSArray *vertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_contentView]|"
                                                                options:0
                                                                metrics:nil
                                                                  views:NSDictionaryOfVariableBindings(_contentView)];

    [_scrollView addConstraints:horizontal];
    [_scrollView addConstraints:vertical];

    UIView *mainView = self.view;
    horizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_contentView(==mainView)]|"
                                                         options:0
                                                         metrics:nil
                                                           views:NSDictionaryOfVariableBindings(_contentView, mainView)];

    [mainView addConstraints:horizontal];
}

其中 _contentView 是您的 UILabel(如果您在视图容器上放置了更复杂的视图层次结构),而 self.view 是控制器视图(或其他任何东西).希望这也有帮助:iOS使用 UIScrollview 自动布局:为什么滚动视图的内容视图不填充滚动视图?....

Where _contentView is your UILabel (If you have a more complex view hierarchy put on a view container) and self.view is the controller view (or anything else). Hope this helps also: iOS Autolayout with UIScrollview: Why does content view of scroll view not fill the scroll view?....

另外别忘了建立你的UILabel preferredMaxLayoutWidth

干杯!

这篇关于带有自动布局的 UIScrollView 内的动态 UILabel 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:关闭 ModalViewController 后 UIScrollView 的内容发生了变化 下一条:使用自动布局将子视图的宽度与其父视图匹配

相关文章

最新文章