简体   繁体   English

更改UIView高度和子视图y位置[快速]

[英]Change UIView height and subviews y position [Swift]

I'm using auto layout to center my primary UIView horizontally and vertically. 我正在使用自动布局将主UIView水平和垂直居中。 What I'm trying to do is animate the height from 363 to 182 while the subview which has a height of 182 pushes from its current offset to ay offset of 0. 我正在尝试将高度从363设置为182,而子视图的高度为182,将其当前偏移量推向y偏移量0。

In slightly less confusing terminology: The goal is to have the subview towards the bottom, slide over the content at the top of the parent view while the parent view changes its height to the height of the subview. 用稍微混乱的术语:目标是使子视图朝下,在父视图顶部的内容上滑动,同时父视图将其高度更改为子视图的高度。

Changing one before the other seems to break it either way. 先换一个似乎会破坏这两种方法。 My last resort option is to have a view under the subview, that matches the background color of the controller to hide the fact that the parent view doesn't resize, but I'm using a corner radius so it would be kinda ugly that way. 我的最后一个选择是在子视图下有一个视图,该视图与控制器的背景颜色匹配,以隐藏父视图不会调整大小的事实,但是我使用的是拐角半径,因此这种方式有点难看。

I somewhat new to this whole auto layout thing so I'm hoping there's a better solution. 我对整个自动布局有些陌生,所以我希望有一个更好的解决方案。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks :) 谢谢 :)

Presumably you have a height constraint on the parent and a top constraint on the subview. 大概您对父级有一个高度约束,在子视图上有一个顶级约束。

parentHeightConstraint.constant = 182
childTopConstraint.constant = 0
UIView.animateWithDuration(0.3) {
    parentView.window?.layoutIfNeeded()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM