【问题标题】:UILabel height animation with autolayout带有自动布局的 UILabel 高度动画
【发布时间】:2014-02-11 05:46:54
【问题描述】:

我的视图和 UIButton 上有一个 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 ?@"short text":@"long long long text\nlong long long text\nlong long long text\n";
        [UIView animateWithDuration:1.0
                         animations:^{
                             [self.view layoutIfNeeded];
                     }];
}

【问题讨论】:

  • 在触摸按钮的那一刻,文本更改的结果是否正确显示,而您只是想对其进行动画处理?还是按钮的结果框架也错误?
  • @obuseme 结果帧是正确的,但它在没有动画的情况下改变
  • 好的,请看下面我的回答。你只是少了一行
  • @obuseme 我刚刚检查了您的解决方案。 UILabel 帧更改没有动画。看起来像 self.textLabel.text 更改后立即更改框架。

标签: ios uilabel autolayout


【解决方案1】:

在情节提要中定义高度约束,将其添加为 IBOutlet,然后在动画块中更改其值。

【讨论】:

    【解决方案2】:

    在动画块之前,你需要调用[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];
        }];
    }
    

    【讨论】:

      【解决方案3】:

      也许如果你使用

      [self.textLabel sizeToFit];
      

      它有效。

      【讨论】:

        【解决方案4】:

        我有这个例子工作没有问题。

        @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

        【讨论】:

        • 我检查了你的代码。 UILabel 帧变化没有动画。
        猜你喜欢
        • 1970-01-01
        • 2016-02-08
        • 1970-01-01
        • 2014-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-01
        • 1970-01-01
        相关资源
        最近更新 更多