我對我的看法和UIButton有一個UILabel。當按下按鈕UILabel應該改變高度動畫,取決於標籤內容。我正在嘗試:UILabel自動佈局高度動畫
- (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 [email protected]"short text":@"long long long text\nlong long long text\nlong long long text\n";
[UIView animateWithDuration:1.0
animations:^{
[self.view layoutIfNeeded];
}];
}
是否文本變化的結果,在當按鈕被觸動的那一刻正常顯示,而你只是想製作動畫呢?或者,按鈕的結果框架也是錯誤的? –
@obuseme生成的框架是正確的,但它沒有動畫變化 – somedev
好的,請參閱下面的答案。你只是缺少一行 –