Ich benutze Autolayout mit UIScrollView
, um einige Attribute von einem Objekt zu zeigen. Ich lade diese Informationen dynamisch vom Webservice herunter. Der Scrollview hat eine konstante Breite (weil ich kein vertikales Scroll-Verhalten haben möchte) und seine Subviews respektieren diese Breite mit einer Gruppe von Constraints, aber ich kann die Höhe von UILabel
nicht dynamisch erhöhen.Dynamische UILabel Höhe in UIScrollView mit Autolayout
I-Code alles und ich verwende viewDidLoad
Selektor Subviews erstellen ...
- (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;
.
.
.
}
können Sie beobachten die self.detailContentScrollView
Variable, ist dies ein IBOulet
aus View-Controller die Spitze geschaffen.
Dann benutze ich die updateConstraints
Wähler ...
- (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]];
.
.
.
}
und schließlich, wenn ich die Web-Service-Infos über empfangen, ich sende sizeToFit
‚s UILabel
Selektor und layoutIfNeeded
aus dem Scrollview. Aber meine UILabel
Größe ändert sich nie mit dem neuen Inhalt. Was mache ich falsch?
Diese andere funktionelle Lösung für das Problem war, danke. – specktro
Überprüfen Sie diesen Beitrag für eine vollständige Implementierung: http://arielelkin.github.io/articles/uilabel-plus-uiscrollview-plus-autolayout/ – Eric
@Eric: Link ist down. – testing