ios - 如何使用带有自动布局约束的动画移动 View 在 iOS 上

标签 ios objective-c storyboard autolayout uiviewanimation

我正在执行一项任务,我需要将 View 移动 35 像素以显示该 View 下方的 View 。

或者 你可以说我需要在顶部插入一个类似通知的栏,并在运行时将现有 View 移动 35 像素。

问题在于为了支持所有屏幕而启用了自动布局。换档没有按照我们的意愿发生......这里有什么建议吗? 顶 View 有一个约束,它与顶 View 的距离为零,当我们显示下面的 View 时,它需要在运行时更改。

这是移动前的屏幕: enter image description here

移动并显示图像后:

enter image description here

我也给出了侧边栏 View 。 NotifView :我们需要在运行时显示的黄色 View twoView :我们需要切换到在顶部显示 notifview。

-(void)shiftTwoView:(NSString*)总结{

CGFloat shiftHeight = 35.0f;

CGRect twoRect = twoView.frame;
    twoRect.origin.y += shiftHeight;
    twoRect.size.height -= shiftHeight;


[UIView animateWithDuration:3.5
                 animations:^{
                     twoView.frame = twoRect;
                 }];

最佳答案

当您使用自动布局时,您不需要那样设置框架。您可以通过编程方式操作约束值来执行此操作。由于您还没有明确说明您在此处应用了哪些约束,我将解释您需要这样做的所有内容。

notifyview: - leading = 0, trailing = 0, top = 0, height = 0;  
twoView: - leading = 0, trailing = 0, top(to notifview) = 0; bottom = 0;

现在为 notifyview 的高度约束创建一个 IBOutlet,假设是 notifyViewHeightConstraint

现在要显示 notifyView 的内容,您需要做的就是增加它的高度。默认情况下,此高度是 Storyboard 中的 0。您可以将其更改为 notifyViewHeightConstraint.constant = 40;。完成后需要再次消失,可以将constant值设置回0;

-(void)shiftTwoView:(NSString*)summary  
{  
     notifyViewHeightConstraint.constant = 40;   
    [UIView animateWithDuration:2.0 animations:^{  
            [self.view layoutIfNeeded];  
    }]; 
}

关于ios - 如何使用带有自动布局约束的动画移动 View 在 iOS 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34082458/

相关文章:

iphone - 如何避免 UIToolbar TintColor 改变 UIButtonItem 按钮颜色?

ios - react-native 的 Realm 给出 IOS 版本警告

objective-c - Objective C - iOS,在屏幕外的静态表格单元格内迭代 UILabels

ios - Storyboard和续集 : How does the presenting VC know when the presented VC dismisses itself?

ios - 添加libRxAutomationUni_411.a库时出现链接器错误

ios - supportedInterfaceOrientations 未在大尺寸设备中调用

ios - 不同iOS版本之间的应用程序迁移

ios - 关闭应用程序后NSTimer是否运行

ios - 当 "Hide Bottom Bar on Push"对于来自 Cell 的 segue 为真时,UITableView 跳转到顶部显示 SearchBar

swift - 如何使用 Storyboard ID 滑动 View Controller