Search code examples
iosswiftanimationuinavigationcontrolleruicontainerview

Prevent UINavigationBar animation in custom container


I followed this guide Implementing a Container View Controller to make a container that would handle login/logout in the app.

Children view controllers are: UINavigationController for login, and UITabBarController for the rest of the app:

diagram

My problem is that UINavigationBar animates strangely, and I want to prevent its animation:

transition

The animation code is basically this (full project code here):

    let current = childViewControllers.first
    current?.willMoveToParentViewController(nil)

    child.securityContainer = self
    addChildViewController(child)

        child.view.frame = newChildOriginFrame

        UIView.transitionWithView(view, duration: 0.3, options: [], animations: {

            child.view.frame = newChildTargetFrame
            current?.view.frame = oldChildTargetFrame

            self.view.addSubview(child.view)

        }, completion: { _ in

            child.didMoveToParentViewController(self)
            current?.view.removeFromSuperview()
            current?.removeFromParentViewController()
            current?.securityContainer = nil
        })

How can I prevent animation of UINavigationBar?


Solution

  • Fixed it by moving addSubview outside animations block:

    let current = childViewControllers.first
    current?.willMoveToParentViewController(nil)
    
    child.securityContainer = self
    addChildViewController(child)
    
        child.view.frame = newChildOriginFrame
    
        view.addSubview(child.view)
    
        UIView.transitionWithView(view, duration: 0.3, options: [], animations: {
    
            child.view.frame = newChildTargetFrame
            current?.view.frame = oldChildTargetFrame
    
        }, completion: { _ in
    
            child.didMoveToParentViewController(self)
            current?.view.removeFromSuperview()
            current?.removeFromParentViewController()
            current?.securityContainer = nil
        })
    

    (full project source code)