- (voID)vIEwDIDLoad { self.textLabel= [[UILabel alloc] initWithFrame:CGRectZero]; self.textLabel.numberOflines=0; self.textLabel.Font= [UIFont systemFontOfSize:14]; self.textLabel.backgroundcolor= [UIcolor lightGraycolor]; self.textLabel.text= @"short text"; [self.vIEw addSubvIEw:self.textLabel]; [self.textLabel setTranslatesautoresizingMaskIntoConstraints:NO]; [self.vIEw addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[_textLabel]-10-|" options:0 metrics:nil vIEws:NSDictionaryOfVariableBindings(_textLabel)]]; self.button= [UIbutton buttonWithType:UIbuttonTypeSystem]; [self.button addTarget:self action:@selector(buttontouched:) forControlEvents:UIControlEventtouchUpInsIDe]; [self.button setTitle:@"Tap" forState:UIControlStatenormal]; self.button.backgroundcolor= [UIcolor greencolor]; [self.button setTranslatesautoresizingMaskIntoConstraints:NO]; [self.vIEw addSubvIEw:self.button]; [self.vIEw addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[_button]-10-|" options:0 metrics:nil vIEws:NSDictionaryOfVariableBindings(_button)]]; [self.vIEw addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[_textLabel(>=0)]-10-[_button(==20)]" options:0 metrics:nil vIEws:NSDictionaryOfVariableBindings(_textLabel,_button)]];}- (voID)buttontouched:(ID)buttontouched { self.shortText =!self.shortText; self.textLabel.text= self.shortText ?@"short text":@"long long long text\nlong long long text\nlong long long text\n"; [UIVIEw animateWithDuration:1.0 animations:^{ [self.vIEw layoutIfNeeded]; }];}解决方法 在动画块之前,您需要调用[self.vIEw setNeedsUpdateConstraints]以触发告诉视图在调用layoutIfNeeded时需要更新约束
所以新方法:
- (voID)buttontouched:(ID)buttontouched { self.shortText =!self.shortText; self.textLabel.text= self.shortText ?@"short text":@"long long long text\nlong long long text\nlong long long text\n"; [self.vIEw setNeedsUpdateConstraints]; [UIVIEw animateWithDuration:1.0 animations:^{ [self.vIEw layoutIfNeeded]; }];}总结
以上是内存溢出为你收集整理的ios – 具有自动布局的UILabel高度动画全部内容,希望文章能够帮你解决ios – 具有自动布局的UILabel高度动画所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)