【问题标题】:Slide In A UIView by updating constraints通过更新约束滑入 UIView
【发布时间】:2017-11-03 11:23:31
【问题描述】:

我正在尝试通过更新其约束来为 UIView 设置动画。我想最初将视图放在屏幕底部(屏幕外),然后将顶部约束设置为 0,以便动画显示视图向上移动。

我已经尝试了以下没有运气,动画不正确。

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
//CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

self.viewSearchWrapper.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:self.viewSearchWrapper];

NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:self.viewSearchWrapper
                                   attribute:NSLayoutAttributeTrailing
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:self.view
                                   attribute:NSLayoutAttributeTrailing
                                   multiplier:1.0f
                                   constant:0.f];

// Leading

NSLayoutConstraint *leading = [NSLayoutConstraint
                                   constraintWithItem:self.viewSearchWrapper
                                   attribute:NSLayoutAttributeLeading
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:self.view
                                   attribute:NSLayoutAttributeLeading
                                   multiplier:1.0f
                                   constant:0.f];

// Bottom
NSLayoutConstraint *bottom = [NSLayoutConstraint
                                  constraintWithItem:self.viewSearchWrapper
                                  attribute:NSLayoutAttributeBottom
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.view
                                  attribute:NSLayoutAttributeBottom
                                  multiplier:1.0f
                                  constant:0.f];

// Bottom
NSLayoutConstraint *top = [NSLayoutConstraint
                              constraintWithItem:self.viewSearchWrapper
                              attribute:NSLayoutAttributeTop
                              relatedBy:NSLayoutRelationEqual
                              toItem:self.view
                              attribute:NSLayoutAttributeTop
                              multiplier:1.0f
                              constant:screenHeight];

[self.view addConstraint:trailing];
[self.view addConstraint:bottom];
[self.view addConstraint:leading];
[self.view addConstraint:top];                        

[self.viewSearchWrapper setNeedsUpdateConstraints];

[top setConstant:0.f];

[UIView animateWithDuration:1 animations:^{
    [self.view layoutIfNeeded];
}];

【问题讨论】:

  • 您只是在减少顶部约束。尝试同时增加底部约束。或者您可以设置greaterThanOrEqual 关系而不是equal 作为底部约束。

标签: ios objective-c autolayout


【解决方案1】:

添加layoutIfNeeded 以首先将初始约束设置为viewSearchWrapper

// .... keep the code assigning the constraints as it is.

[self.view layoutIfNeeded];  // This will set the initial frame of viewSearchWrapper
[self.viewSearchWrapper setNeedsUpdateConstraints];
[top setConstant:0.f];
[UIView animateWithDuration:1 animations:^{
   [self.view layoutIfNeeded];
}];

【讨论】:

    【解决方案2】:

    animateWithDuration里面添加[top setConstant:0.f]如下图:

    [UIView animateWithDuration:1 animations:^{
            [top setConstant:0.f];
            [self.view layoutIfNeeded];
        }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 2019-10-31
      • 1970-01-01
      • 2021-04-10
      相关资源
      最近更新 更多