Cómo ocultar la barra de pestañas con animación en iOS?


Así que tengo un botón que está conectado a una IBAction. Cuando presiono el botón quiero ocultar la barra de pestañas en mi aplicación iOS con una animación. Esto [self setTabBarHidden:hidden animated:NO]; o esto [self.tabBarController setTabBarHidden:hidden animated:YES]; no funciona. Este es mi código sin la animación:

- (IBAction)picture1:(id)sender {
    [self.tabBarController.tabBar setHidden:YES];
}

Cualquier ayuda sería muy apreciada: D

Author: neilco, 2014-01-05

11 answers

Trato de mantener las animaciones de vista disponibles para mí usando la siguiente fórmula:

// pass a param to describe the state change, an animated flag and a completion block matching UIView animations completion 
- (void)setTabBarVisible:(BOOL)visible animated:(BOOL)animated completion:(void (^)(BOOL))completion {

    // bail if the current state matches the desired state
    if ([self tabBarIsVisible] == visible) return (completion)? completion(YES) : nil;

    // get a frame calculation ready
    CGRect frame = self.tabBarController.tabBar.frame;
    CGFloat height = frame.size.height;
    CGFloat offsetY = (visible)? -height : height;

    // zero duration means no animation
    CGFloat duration = (animated)? 0.3 : 0.0;

    [UIView animateWithDuration:duration animations:^{
        self.tabBarController.tabBar.frame = CGRectOffset(frame, 0, offsetY);
    } completion:completion];
}

//Getter to know the current state
- (BOOL)tabBarIsVisible {
    return self.tabBarController.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame);
}

//An illustration of a call to toggle current state
- (IBAction)pressedButton:(id)sender {
    [self setTabBarVisible:![self tabBarIsVisible] animated:YES completion:^(BOOL finished) {
        NSLog(@"finished");
    }];
}
 71
Author: danh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-12 09:19:04

Cuando se trabaja con storyboard, es fácil configurar el Controlador de vista para ocultar la barra de pestañas al empujar, en el controlador de vista de destino, simplemente seleccione esta casilla de verificación:
introduzca la descripción de la imagen aquí

 87
Author: Ben,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-31 07:11:04

Según Apple docs, hidesBottomBarWhenPushed propiedad de UIViewController, un valor booleano, que indica si la barra de herramientas en la parte inferior de la pantalla está oculta cuando el controlador de vista se empuja a un controlador de navegación.

El valor de esta propiedad en el controlador de vista superior determina si la barra de herramientas es visible.

El enfoque recomendado para ocultar la barra de pestañas sería el siguiente

    ViewController *viewController = [[ViewController alloc] init];
    viewController.hidesBottomBarWhenPushed = YES;  // This property needs to be set before pushing viewController to the navigationController's stack. 
    [self.navigationController pushViewController:viewController animated:YES];

Sin embargo, tenga en cuenta que este enfoque solo se aplicará a ViewController y no se propagará a otros controladores de vista a menos que comience a configurar la misma propiedad hidesBottomBarWhenPushed en otros controladores de vista antes de empujarla a la pila del controlador de navegación.

 28
Author: ldindu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-05 16:18:28

Versión Swift 3.0, usando una extensión:

extension UITabBarController {

    private struct AssociatedKeys {
        // Declare a global var to produce a unique address as the assoc object handle
        static var orgFrameView:     UInt8 = 0
        static var movedFrameView:   UInt8 = 1
    }

    var orgFrameView:CGRect? {
        get { return objc_getAssociatedObject(self, &AssociatedKeys.orgFrameView) as? CGRect }
        set { objc_setAssociatedObject(self, &AssociatedKeys.orgFrameView, newValue, .OBJC_ASSOCIATION_COPY) }
    }

    var movedFrameView:CGRect? {
        get { return objc_getAssociatedObject(self, &AssociatedKeys.movedFrameView) as? CGRect }
        set { objc_setAssociatedObject(self, &AssociatedKeys.movedFrameView, newValue, .OBJC_ASSOCIATION_COPY) }
    }

    override open func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        if let movedFrameView = movedFrameView {
            view.frame = movedFrameView
        }
    }

    func setTabBarVisible(visible:Bool, animated:Bool) {
        //since iOS11 we have to set the background colour to the bar color it seams the navbar seams to get smaller during animation; this visually hides the top empty space...
        view.backgroundColor =  self.tabBar.barTintColor 
        // bail if the current state matches the desired state
        if (tabBarIsVisible() == visible) { return }

        //we should show it
        if visible {
            tabBar.isHidden = false
            UIView.animate(withDuration: animated ? 0.3 : 0.0) {
                //restore form or frames
                self.view.frame = self.orgFrameView!
                //errase the stored locations so that...
                self.orgFrameView = nil
                self.movedFrameView = nil
                //...the layoutIfNeeded() does not move them again!
                self.view.layoutIfNeeded()
            }
        }
            //we should hide it
        else {
            //safe org positions
            orgFrameView   = view.frame
            // get a frame calculation ready
            let offsetY = self.tabBar.frame.size.height
            movedFrameView = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height + offsetY)
            //animate
            UIView.animate(withDuration: animated ? 0.3 : 0.0, animations: {
                self.view.frame = self.movedFrameView!
                self.view.layoutIfNeeded()
            }) {
                (_) in
                self.tabBar.isHidden = true
            }
        }
    }

    func tabBarIsVisible() ->Bool {
        return orgFrameView == nil
    }
}
  • Esto se basa en la entrada de Sherwin Zadeh después de unas horas de juego.
  • En lugar de mover la barra de pestañas en sí se mueve el marco de la vista, esto efectivamente desliza la barra de pestañas muy bien fuera de la parte inferior de la pantalla, pero...
  • ... tiene la ventaja de que el contenido que se muestra dentro del UITabBarController también está tomando la pantalla completa!
  • tenga en cuenta que también está usando el AssociatedObject funcionalidad a los datos adjuntos a la UIView sin subclase y por lo tanto una extensión es posible (extensiones no permiten propiedades almacenadas)

introduzca la descripción de la imagen aquí

 21
Author: HixField,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-22 06:44:12

Versión Swift:

@IBAction func tap(sender: AnyObject) {
    setTabBarVisible(!tabBarIsVisible(), animated: true, completion: {_ in })
}


// pass a param to describe the state change, an animated flag and a completion block matching UIView animations completion
func setTabBarVisible(visible: Bool, animated: Bool, completion:(Bool)->Void) {

    // bail if the current state matches the desired state
    if (tabBarIsVisible() == visible) {
        return completion(true)
    }

    // get a frame calculation ready
    let height = tabBarController!.tabBar.frame.size.height
    let offsetY = (visible ? -height : height)

    // zero duration means no animation
    let duration = (animated ? 0.3 : 0.0)

    UIView.animateWithDuration(duration, animations: {
        let frame = self.tabBarController!.tabBar.frame
        self.tabBarController!.tabBar.frame = CGRectOffset(frame, 0, offsetY);
    }, completion:completion)
}

func tabBarIsVisible() -> Bool {
    return tabBarController!.tabBar.frame.origin.y < CGRectGetMaxY(view.frame)
}
 9
Author: Sherwin Zadeh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-16 01:35:45

Intente establecer el marco de la barra de pestañas en la animación. Ver este tutorial.

Solo tenga en cuenta, es una mala práctica hacer eso, debe establecer mostrar/ocultar la barra de pestañas cuando UIViewController push by set the property hidesBottomBarWhenPushed a YES.

 3
Author: gran33,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-05 15:38:29

Reescriba la respuesta de Sherwin Zadeh en Swift 4:

/* tab bar hide/show animation */
extension AlbumViewController {
    // pass a param to describe the state change, an animated flag and a completion block matching UIView animations completion
    func setTabBarVisible(visible: Bool, animated: Bool, completion: ((Bool)->Void)? = nil ) {

        // bail if the current state matches the desired state
        if (tabBarIsVisible() == visible) {
            if let completion = completion {
                return completion(true)
            }
            else {
                return
            }
        }

        // get a frame calculation ready
        let height = tabBarController!.tabBar.frame.size.height
        let offsetY = (visible ? -height : height)

        // zero duration means no animation
        let duration = (animated ? kFullScreenAnimationTime : 0.0)

        UIView.animate(withDuration: duration, animations: {
            let frame = self.tabBarController!.tabBar.frame
            self.tabBarController!.tabBar.frame = frame.offsetBy(dx: 0, dy: offsetY)
        }, completion:completion)
    }

    func tabBarIsVisible() -> Bool {
        return tabBarController!.tabBar.frame.origin.y < view.frame.maxY
    }
}
 3
Author: Bill Chan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-20 10:41:52

Probado en swift 3.0 / iOS10 / Xcode 8:

    self.tabBarController?.tabBar.isHidden = true

Lo configuro cuando se muestra mi controlador: (y Escondo cuando esté atrás, después de la navegación)

override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.isHidden = false

    }

    override func viewWillDisappear(_ animated: Bool) {
                super.viewWillDisappear(animated)
        self.tabBarController?.tabBar.isHidden = true

    }

POR cierto: es mejor tener una bandera para guardar si se muestra o no, ya que otros respiraderos pueden desencadenar ocultar/mostrar

 1
Author: ingconti,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-11 07:42:18

Desafortunadamente, no puedo comentar la respuesta de HixField porque no tengo suficiente reputación, así que tengo que dejar esto como una respuesta separada.

A su respuesta le falta la propiedad calculada para movedFrameView, que es:

var movedFrameView:CGRect? {
  get { return objc_getAssociatedObject(self, &AssociatedKeys.movedFrameView) as? CGRect }
  set { objc_setAssociatedObject(self, &AssociatedKeys.movedFrameView, newValue, .OBJC_ASSOCIATION_COPY) }
}
 1
Author: Bryan Bartow,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-04 21:05:05

Basado en la respuesta de HixField y Bil Chan he preparado una versión que funciona en iOS 9 a iOS 11 (no puedo probar en otras versiones en este momento), y iPhone 4S a iPhone X (probado). Cuando desee ocultar la barra de pestañas, simplemente llame a: tabBarController?.set(visible:false, animated: true), de manera similar si desea mostrar call: tabBarController?.set(visible:true, animated: true). También puedes encontrar este código en mi síntesis: https://gist.github.com/MaciejGad/9a4d1f65dcf382373911c90c548d2713

extension UITabBarController {

func set(visible: Bool, animated: Bool, completion: ((Bool)->Void)? = nil ) {

    guard isVisible() != visible else {
        completion?(true)
        return
    }

    let offsetY = tabBar.frame.size.height
    let duration = (animated ? 0.3 : 0.0)

    let beginTransform:CGAffineTransform
    let endTransform:CGAffineTransform

    if visible {
        beginTransform = CGAffineTransform(translationX: 0, y: offsetY)
        endTransform = CGAffineTransform.identity
    } else {
        beginTransform = CGAffineTransform.identity
        endTransform = CGAffineTransform(translationX: 0, y: offsetY)
    }

    tabBar.transform = beginTransform
    if visible {
        tabBar.isHidden = false
    }

    UIView.animate(withDuration: duration, animations: {
        self.tabBar.transform = endTransform
    }, completion: { compete in
        completion?(compete)
        if !visible {
            self.tabBar.isHidden = true
        }
    })
}

func isVisible() -> Bool {
    return !tabBar.isHidden
}
}
 -1
Author: bazyl87,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-30 13:09:15

Esto me duele: [self.tabBar setHidden:YES];
donde self es el controlador de vista, TabBar es el id de la TabBar.

 -6
Author: us_david,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-18 06:07:11