ytpm
ytpm

Reputation: 5150

Hiding a view with constraints and animation

I have a UIViewController which is a custom TabBarController. Inside I have a UIView which holds UIViewController and at the bottom another UIView which function as a TabBar, I want to hide the TabBar with animation and keep the constraints in order. For some reason, every time I try to do it, the view constraints getting all messed up.

The TabBar has constant height of 100points.

- UIViewController
  - viewContent (UIView, the UIViewController container)
  - viewTabBar (UIView as TabBar)

enter image description here

This is my code:

func hideTabBar() {
    UIView.animate(withDuration: 400) {
        self.contentView.frame.size.height += self.viewTabBar.frame.size.height
        self.view.layoutIfNeeded()
    }
}

Help?

Upvotes: 1

Views: 485

Answers (1)

Thijs van der Heijden
Thijs van der Heijden

Reputation: 1176

I'm guessing the best way to do this would be to either make the tabbar height constraint an IBOutlet, or just create it programmatically, and then just change that constraint constant value. Something like this:

var tabbarHeightConstraint = NSLayoutConstraint(item: tabbarView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .none, multiplier: 1, constant: 100)

NSLayoutConstraint.activate([tabbarHeightConstraint])

// Animating, set new tab bar height to 0
tabbarHeightConstraint.constant = 0

UIView.animate(withDuration: 400) {
  self.view.layoutIfNeeded() // Or wherever the tabbar view is in
}

Now if you want to show the tab bar again, just do the same but set the constant to 100.

Upvotes: 2

Related Questions