Varun Mehta
Varun Mehta

Reputation: 1753

Animate subview on UIViewController view

I have UITableView as a subview over UIView (parent view) of UIViewController. When i'm animating my parent view as below:

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

        })

I observed that my subview (table view) is not animating. I tried setting Autoresizing Mask of subviews to flexibleWidth and flexibleHeight, but din't get any success. Anybody having any idea why its happening.

Upvotes: 0

Views: 775

Answers (1)

Woodstock
Woodstock

Reputation: 22966

If you are using auto layout (which is the default in iOS 8/9 on Xcode 6/7) you need to change the constraints of your view inside he UIView animation closure, instead of changing the frame.

If you are not using auto layout, then you can update the frame as above, however, I'd imagine you are. Thus you need to call layoutSubviews, then programatically set new constraints that represent your desired change to the layout, and finally call layoutSubviews again, inside the animation closure.

Example:

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

Upvotes: 1

Related Questions