UIViewController 视图上的动画子视图

Animate subview on UIViewController view

我将 UITableView 作为 UIViewController 的 UIView(父视图)的子视图。当我如下动画我的父视图时:

       UIView.animateWithDuration(duration, animations: {
            viewController?.view.frame = CGRectMake(0,0,320,568)
        }, completion: {_ in

        })

我发现我的子视图(table 视图)没有动画。我尝试将子视图的 Autoresizing Mask 设置为 flexibleWidth 和 flexibleHeight,但没有取得任何成功。任何人都知道为什么会这样。

如果您正在使用自动布局(这是 iOS 8/9 和 Xcode 6/7 中的默认设置),您需要更改视图内部的约束 UIView 动画关闭,而不是改变帧。

如果您没有使用自动布局,那么您可以像上面那样更新框架,但是,我想您是。因此,您需要调用 layoutSubviews,然后以编程方式设置代表您对布局进行的所需更改的新约束,最后在动画闭包内再次调用 layoutSubviews

示例:

func AnimateBackgroundHeight() {
    self.view.layoutSubviews()
    UIView.animateWithDuration(0.5, animations: {
         self.heightCon.constant = 600 // heightCon is the IBOutlet to the constraint
         self.view.layoutSubviews()    
    })
}