2014-01-21 164 views
5

我對我的看法和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]; 
        }]; 
} 
+0

是否文本變化的結果,在當按鈕被觸動的那一刻正常顯示,而你只是想製作動畫呢?或者,按鈕的結果框架也是錯誤的? –

+0

@obuseme生成的框架是正確的,但它沒有動畫變化 – somedev

+0

好的,請參閱下面的答案。你只是缺少一行 –

回答

0

定義故事板中的高度限制,將其添加爲IBOutlet,然後在動畫塊內更改它的值。

5

右鍵動畫塊之前,你需要調用[self.view setNeedsUpdateConstraints]才能觸發告訴認爲,制約因素需要更新,當你調用layoutIfNeeded

因此,新的方法:

- (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"; 
    [self.view setNeedsUpdateConstraints]; 
    [UIView animateWithDuration:1.0 
        animations:^{ 
         [self.view layoutIfNeeded]; 
    }]; 
} 
0

也許如果您使用

[self.textLabel sizeToFit]; 

它的工作原理。

1

我有這個例子工作沒有問題。

@interface ViewController() 
 

 
@end 
 

 
@implementation ViewController 
 
{ 
 
    __weak UIView *_bgView; 
 
    __weak UILabel *_label; 
 
} 
 

 
- (void)viewDidLoad { 
 
    [super viewDidLoad]; 
 
    // Do any additional setup after loading the view, typically from a nib. 
 
    
 
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectZero]; 
 
    bgView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5]; 
 
    bgView.translatesAutoresizingMaskIntoConstraints = NO; 
 
    [self.view addSubview:bgView]; 
 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bgView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(bgView)]]; 
 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bgView]|" options:0 metrics:nil views: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 = NO; 
 
    [bgView addSubview:label]; 
 
    [bgView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[label]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]]; 
 
    [bgView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[label]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]]; 
 
    _label = label; 
 
    
 
    [self performSelector:@selector(animate) withObject:nil afterDelay:1]; 
 
} 
 

 
- (void)animate 
 
{ 
 
    _label.text = @"ahoj, ahoj\ntotalka ahoj"; 
 
    
 
    [UIView animateWithDuration:1 animations:^{ 
 
     [_bgView.superview layoutIfNeeded]; 
 
    } completion:^(BOOL finished) { 
 
     ; 
 
    }]; 
 
} 
 

 
- (void)didReceiveMemoryWarning { 
 
    [super didReceiveMemoryWarning]; 
 
    // Dispose of any resources that can be recreated. 
 
} 
 

 
@end

+0

我檢查了你的代碼。 UILabel框架更改無需動畫。 – somedev