Cambiar la altura de UITabBar


Utilizo UITabBarController como una vista raíz y la aplicación es compatible con iOS 6 y superiores. La jerarquía de clases del proyecto es la siguiente.

UITabBarController
  - tab1
    - UINavigationController
      - UIViewController
      - UIViewController
      .
      .
  - tab2
    - UINavigationController
      - UIViewController
      - UIViewController
      .
      .
      .
  - tab3
    - UIViewController
  - tab4
    - UIViewController

Utilicé el código inferior para cambiar la altura de UITabBar en uno de los controladores UIViewController (que está dentro de UINavigationController) en la jerarquía superior.

CGRect tabbarFrame = self.tabBarController.tabBar.frame;
tabbarFrame.size.height += 60;
self.tabBarController.tabBar.frame = tabbarFrame;

Pero no está cambiando la altura. UITabBar se muestra con la altura predeterminada. Aunque el registro de su valor imprime el valor cambiado como se muestra a continuación.

<UITabBar: 0xb528f60; frame = (0 431; 320 109); autoresize = W+TM; layer = <CALayer: 0xb529080>>

¿Cómo puedo cambiar la altura de UITabBar para lograr algo como esto:?

introduzca la descripción de la imagen aquí

Author: sasquatch, 2014-04-13

15 answers

La altura de la barra de pestañas es una constante establecida por Apple, por lo que no puede cambiarla.

 -57
Author: Codengine,
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-12-19 11:40:38

Me enfrenté a este problema y fui capaz de resolverlo.

Tienes que añadir el siguiente código a tu subclase de la clase UITabBarController.

const CGFloat kBarHeight = 80;

- (void)viewWillLayoutSubviews {

    CGRect tabFrame = self.tabBar.frame; //self.TabBar is IBOutlet of your TabBar
    tabFrame.size.height = kBarHeight;
    tabFrame.origin.y = self.view.frame.size.height - kBarHeight;
    self.tabBar.frame = tabFrame;
}
 116
Author: Rushikesh,
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-08-24 20:17:01

Para iOS 8.2, Xcode 6.2 Swift idioma:

Cree un "DNMainTabVC.swift " (DeveloperNameMainTabViewController.swift file) para su UITabBarController (de tipo UITabBarController) y conéctelo a su storyboard VC.

Añádanse las siguientes líneas:

override func viewWillLayoutSubviews() {
    var tabFrame = self.tabBar.frame
    // - 40 is editable , the default value is 49 px, below lowers the tabbar and above increases the tab bar size
    tabFrame.size.height = 40
    tabFrame.origin.y = self.view.frame.size.height - 40
    self.tabBar.frame = tabFrame
}

Esto funcionó para mí.

 39
Author: MB_iOSDeveloper,
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-08-27 21:36:00

Swift3. 0, compatible con Swift 4.0

Pre-iPhone X altura predeterminada de la barra de pestañas: 49pt

IPhone X altura predeterminada de la barra de pestañas: 83pt

Una solución universal compatible con todos los dispositivos iOS, incluido iPhone X El tamaño de la pantalla se vería así:

  1. Captura la altura predeterminada de UITabBar:

    fileprivate lazy var defaultTabBarHeight = { tabBar.frame.size.height }()
    
  2. Ajuste la altura de UITabBar:

        override func viewWillLayoutSubviews() {
            super.viewWillLayoutSubviews()
    
            let newTabBarHeight = defaultTabBarHeight + 16.0
    
            var newFrame = tabBar.frame
            newFrame.size.height = newTabBarHeight
            newFrame.origin.y = view.frame.size.height - newTabBarHeight
    
            tabBar.frame = newFrame
        }
    
 29
Author: Kiarash Asar,
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-13 06:23:51

Crea una subclase personalizada de tipo UITabBar, luego implementa el siguiente método:

@implementation CustomTabBar
#define kTabBarHeight = // Input the height we want to set for Tabbar here
-(CGSize)sizeThatFits:(CGSize)size
{
    CGSize sizeThatFits = [super sizeThatFits:size];
    sizeThatFits.height = kTabBarHeight;

    return sizeThatFits;
}
@end

Espero que esto funcione.

 24
Author: silentBeep,
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-12-19 12:26:26

Probado en XCode 9.0 y Swift 4

Como se sugirió en respuestas anteriores-heredar UITabBar y anular sizeThatFits, pero marcar height como @IBInspectable, por lo que podría establecerse en el Constructor de interfaz :

import UIKit

class CustomTabBar : UITabBar {
    @IBInspectable var height: CGFloat = 0.0

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        var sizeThatFits = super.sizeThatFits(size)
        if height > 0.0 {
            sizeThatFits.height = height
        }
        return sizeThatFits
    }
}

Establece la clase CustomTabBar para el UITabBar en el Inspector de Identidad (⌥⌘3):

Inspector de Identidad de Barra de Pestañas

Luego establezca el Height deseado (mayor que 0.0) en el Inspector de atributos (⌥⌘4):

Inspector de Atributos de Barra de Pestañas

 21
Author: msrdjan,
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-26 11:43:44

Swift 2.0:

var tabBar:UITabBar?

override func viewWillLayoutSubviews() {
    var tabFrame: CGRect = self.tabBar!.frame
    tabFrame.size.height = 60
    tabFrame.origin.y = self.view.frame.size.height - 60
    self.tabBar!.frame = tabFrame
}
 11
Author: A.G,
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-02-11 12:16:57

Para Swift 4

extension UITabBar {

     override open func sizeThatFits(_ size: CGSize) -> CGSize {
     var sizeThatFits = super.sizeThatFits(size)
     sizeThatFits.height = 60 // adjust your size here
     return sizeThatFits
    }
 }
 5
Author: Ramazan Karami,
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-02-12 10:11:32

Aprovechando las respuestas anteriores y actualizando Swift 3.

Subclase UITabController y asegúrese de asignar su nueva clase personalizada al Inspector de Identidad de su UITabController.

Swift 3.0

class MainTabBarController: UITabBarController {

    override func viewWillLayoutSubviews() {
        var newTabBarFrame = tabBar.frame

        let newTabBarHeight: CGFloat = 60
        newTabBarFrame.size.height = newTabBarHeight
        newTabBarFrame.origin.y = self.view.frame.size.height - newTabBarHeight

        tabBar.frame = newTabBarFrame
    }
}

Aviso: si tienes un espacio en blanco debajo de la barra de pestañas, asegúrate de haber puesto este código en viewWillLayoutSubviews () y no en viewDidLoad ().

 4
Author: Kqtr,
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-05-31 19:56:25

Implementación de Xamarin:

public override void ViewWillLayoutSubviews()
{
    base.ViewWillLayoutSubviews();
    const float newTabBarHeight = 40f;
    TabBar.Frame = new CGRect(TabBar.Frame.X, TabBar.Frame.Y + (TabBar.Frame.Height - newTabBarHeight), TabBar.Frame.Width, newTabBarHeight);
}
 4
Author: Alexey Strakh,
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-07-30 21:47:53

Swift 3.0 + Reemplace 200 a la altura deseada en el siguiente código.

   extension UITabBar {
        override open func sizeThatFits(_ size: CGSize) -> CGSize {
            return CGSize(width: UIScreen.main.bounds.width, height: 200)
        }
    }
 4
Author: William Hu,
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-24 14:42:13

Puede modificar la altura de la barra de pestañas subclasificándola. En realidad lo hice hace mucho tiempo. xcode 6.0

override func sizeThatFits(_ size: CGSize) -> CGSize {
    return CGSize(width: super.sizeThatFits(size).width, height: 60)
}

Que debe devolver su ancho predeterminado con la altura de 60pts.

 3
Author: nferocious76,
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-02-18 02:00:04

, Por alguna razón, la respuesta de @Rushikesh estaba funcionando bastante bien hasta que iOS 10 pero he tenido algunos problemas con iOS 11 y Swift 3.2.

La barra de pestañas estaba cambiando su marco cada vez que tocaba una nueva pestaña.

Arreglé esto poniendo el código en la función viewDidLayoutSubviews() en lugar de viewWillLayoutSubviews()

Swift 3 :

override func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()
    var tabFrame            = tabBar.frame
    tabFrame.size.height    = 65
    tabFrame.origin.y       = view.frame.size.height - 65
    tabBar.frame            = tabFrame
}
 1
Author: S. Azzopardi,
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 13:27:07

Esta es también una manera de hacer eso

extension UITabBar {

override public func sizeThatFits(size: CGSize) -> CGSize {
    super.sizeThatFits(size)
    var sizeThatFits = super.sizeThatFits(size)
    sizeThatFits.height = 71
    return sizeThatFits
} }
 1
Author: Sultan Ali,
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-03-20 18:48:41

IPhoneX tiene una altura diferente, por lo que si nos movemos a una altura más pequeña, la forma de la barra de pestañas será mala en iPhoneX

- (void)viewWillLayoutSubviews
{
    int requiredHeight = 55;
    CGRect tabFrame = self.tabBar.frame;
    if (tabFrame.size.height < requiredHeight)
    {
        tabFrame.size.height = requiredHeight;
        tabFrame.origin.y = self.view.frame.size.height - requiredHeight;
        self.tabBar.frame = tabFrame;
    }
}
 0
Author: Abeer Iqbal,
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-03-21 16:37:50