Ios 带自动布局的UILabel高度动画
我的视图上有一个UILabel和UIButton。当按下按钮时,标签的高度应改变动画,这取决于标签内容。我试着这样做:Ios 带自动布局的UILabel高度动画,ios,uilabel,autolayout,Ios,Uilabel,Autolayout,我的视图上有一个UILabel和UIButton。当按下按钮时,标签的高度应改变动画,这取决于标签内容。我试着这样做: - (void)viewDidLoad { self.textLabel= [[UILabel alloc] initWithFrame:CGRectZero]; self.textLabel.numberOfLines=0; self.textLabel.font= [UIFont systemFontOfSize:14
- (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];
}];
}
在故事板中定义高度约束,将其添加为IBOutlet,然后在动画块中更改其值。在动画块之前,需要调用
[self.view setNeedsUpdateConstraints]
在调用layoutifneed
因此,新方法:
- (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];
}];
}
也许如果你使用
[self.textLabel sizeToFit];
它是有效的。我有这个例子,没有问题
@界面视图控制器()
@结束
@实现视图控制器
{
__弱UIView*\BGU视图;
__弱UILabel*_标签;
}
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
UIView*bgView=[[UIView alloc]initWithFrame:CGRectZero];
bgView.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5];
bgView.translatesAutoresizingMaskIntoConstraints=否;
[self.view addSubview:bgView];
[self.view addConstraints:[nsLayoutConstraintsWithVisualFormat:@“H:|[bgView]|”选项:0指标:无视图:NSDictionaryOfVariableBindings(bgView)];
[self.view addConstraints:[nsLayoutConstraintsWithVisualFormat:@“V:[bgView]|”选项:0指标:无视图:NSDictionaryOfVariableBindings(bgView)];
_bgView=bgView;
UILabel*label=[[UILabel alloc]initWithFrame:CGRectZero];
label.backgroundColor=[[UIColor greenColor]colorWithAlphaComponent:0.5];
label.text=@“ahoj”;
label.numberOfLines=99;
label.textAlignment=NSTextAlignmentCenter;
label.translatesAutoresizingMaskIntoConstraints=否;
[bgView addSubview:标签];
[bgView addConstraints:[NSLayoutConstraint Constraints with VisualFormat:@“H:|-8-[label]-8-|”选项:0度量:无视图:NSDictionaryOfVariableBindings(label)];
[bgView addConstraints:[NSLayoutConstraint Constraints with VisualFormat:@“V:|-8-[label]-8-|”选项:0度量:无视图:NSDictionaryOfVariableBindings(label)];
_标签=标签;
[自执行选择器:@selector(animate)with object:nil afterDelay:1];
}
-(空)设置动画
{
_label.text=@“ahoj,ahoj\ntotalka ahoj”;
[UIView animateWithDuration:1动画:^{
[\u bgView.superview layoutifneed];
}完成:^(布尔完成){
;
}];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
@结束
触摸按钮时,文本更改的结果是否正确显示,而您只是在尝试为其设置动画?或者按钮的结果帧也是错误的?@obuseme结果帧是正确的,但它在没有animationOk的情况下会更改,请参见下面我的答案。你只是少了一个line@obuseme我刚刚检查了你的解决方案。UILabel帧在没有动画的情况下更改。看起来self.textLabel.text更改后框架立即更改。我已检查了您的代码。UILabel帧在没有动画的情况下更改。