ios objective-c swift uitabbar

ios - Posicionamiento de UITabBar en la parte superior.



objective-c swift (6)

¿Es posible? Claro, pero viola las pautas de la interfaz humana.

Capturas de pantalla:

Código:

TabController.h:

#import <UIKit/UIKit.h> @interface TabController : UITabBarController <UITabBarControllerDelegate> @end

TabController.m:

#import "TabController.h" @interface TabController () @end @implementation TabController - (void)viewDidLoad { [super viewDidLoad]; self.delegate = self; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; [self.tabBar invalidateIntrinsicContentSize]; CGFloat tabSize = 44.0; UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { tabSize = 32.0; } CGRect tabFrame = self.tabBar.frame; tabFrame.size.height = tabSize; tabFrame.origin.y = self.view.frame.origin.y; self.tabBar.frame = tabFrame; // Set the translucent property to NO then back to YES to // force the UITabBar to reblur, otherwise part of the // new frame will be completely transparent if we rotate // from a landscape orientation to a portrait orientation. self.tabBar.translucent = NO; self.tabBar.translucent = YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end

Soy un principiante en el desarrollo de iOS. Mi pregunta es: ¿es posible colocar UITabBar en la parte superior y cómo? No puedo posicionar mi UITabBar en la parte superior de la vista.


Aquí hay un ejemplo de swift 3 del código de aviatorken89.

  1. Primero crea un nuevo archivo.
  2. Seleccione la fuente de cacao de clase táctil.
  3. Designar subclase de: UITabBarController
  4. Nombra la clase "CustomTabBarController" o lo que quieras.
  5. Agregue el siguiente código a la clase.

    override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() var tabFrame:CGRect = self.tabBar.frame tabFrame.origin.y = self.view.frame.origin.y self.tabBar.frame = tabFrame }

  6. Si está utilizando el guión gráfico, asegúrese de cambiar la clase de la barra de pestañas a su clase personalizada a través del "inspector de identidad".


Puede crear una barra de pestañas personalizada al hacerla usted mismo, pero Apple desalienta en gran medida imitar un control del sistema para una función para la cual no estaba destinado originalmente.

Una vez más, te desanimo a que lo hagas, porque viola la consistencia de tu aplicación y las aplicaciones del sistema. Pero ya que estoy en ello, aquí vamos:

Para una barra de pestañas personalizada, debe crear una vista que contenga varios botones. También necesita una vista de contenedor debajo de la UITabBar (porque desea que la UITabBar de UITabBar esté en la parte superior). Cuando se presiona un botón, cambia UIViewController dentro del contenedor.
Es bastante simple, pero por supuesto, no es muy recomendable.


Si quieres algo como una barra de barra de herramientas en la parte superior, entonces, ¿qué tal si creas una vista UIV personalizada y agregas cualquier número de UIButtons en ella? Luego vincule algunos ViewControllers con cada botón a través de Segue. ¡Hecho!

No es necesario personalizar la UITabBar. Como dijo aviatorken89 anteriormente, va en contra de la guía de interfaz humana.


Swift3: Logro esto creando una clase personalizada para UITabBarController :

class CustomTabBarController: UITabBarController { @IBOutlet weak var financialTabBar: UITabBar! override func viewDidLoad() { super.viewDidLoad() // I''ve added this line to viewDidLoad UIApplication.shared.statusBarFrame.size.height financialTabBar.frame = CGRect(x: 0, y: financialTabBar.frame.size.height, width: financialTabBar.frame.size.width, height: financialTabBar.frame.size.height) }

No te olvides de configurar tu clase personalizada en TabBarController

El resultado sería así:


override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height) }

ACTUALIZAR IOS 11 EDICIÓN

el código anterior no funciona en ios 11. así que aquí hay una solución que encontré.

override func viewDidLayoutSubviews() { tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height) super.viewDidLayoutSubviews() }