使用自动布局添加包含内容的 UIViewController 时,内容的动画不起作用



我有一个UIView控制器,我正在将其添加为iPad应用程序中的子视图。UIView控制器有一个UITableView,根据情节提要中设置的自动布局调整大小。我想显示将此视图从iPad的中心移动到右端的动画(横向模式(。为此,我最初将宽度设置为 0,并在动画块中增加它。尽管动画效果很好,但UIViewController中的表视图是在动画之前添加的。所以它不会动画。IT也应该与其父级(UIViewController(一起扩展。我的代码:

@IBAction func tappedNotifications(_ sender: UIButton) {
    let storyboard = UIStoryboard(name: "Content_iPad", bundle: nil)
    let notificationSettingsVC = storyboard.instantiateViewController(withIdentifier:"SettingsViewController_iPad")
    notificationSettingsVC.view.frame = CGRect(x: 380, y: 0, width: 0, height: 775)
    self.view.addSubview(notificationSettingsVC.view)
    notificationSettingsVC.didMove(toParentViewController: self)
    UIView.animate(withDuration: 0.7, animations: {
        notificationSettingsVC.view.frame = CGRect(x: 380, y: 0, width: 0, height: 775)
        notificationSettingsVC.view.frame = CGRect(x: 380, y: 0, width: 570, height: 775)
        notificationSettingsVC.view.setNeedsLayout()
    }, completion: {
        (completed) -> Void in
    })
}

看起来您正在告诉通知视图控制器在动画块内将其宽度动画化为 0 和 570,请尝试删除将宽度设置为 0 的帧更改。

@IBAction func tappedNotifications(_ sender: UIButton) {
    let storyboard = UIStoryboard(name: "Content_iPad", bundle: nil)
    let notificationSettingsVC = storyboard.instantiateViewController(withIdentifier:"SettingsViewController_iPad")
    notificationSettingsVC.view.frame = CGRect(x: 380, y: 0, width: 0, height: 775)
    self.view.addSubview(notificationSettingsVC.view)
    notificationSettingsVC.didMove(toParentViewController: self)
    UIView.animate(withDuration: 0.7, animations: {
        notificationSettingsVC.view.frame = CGRect(x: 380, y: 0, width: 570, height: 775)
        notificationSettingsVC.view.setNeedsLayout()
    }, completion: {
        (completed) -> Void in
    })
}