gpt4 book ai didi

ios - 通过更新约束在 UIView 中滑动

转载 作者:行者123 更新时间:2023-11-28 19:29:36 25 4
gpt4 key购买 nike

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

我试过以下方法,但一点都不走运,动画不正确。

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];
}];

最佳答案

添加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];
}];

关于ios - 通过更新约束在 UIView 中滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47094612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com