Cambiar el título del botón UINavigationBar back


En mi aplicación quiero usar el texto 'Back' como título del botón back para cada viewcontroller. He leído muchos posts en stackoverflow pero no tengo nada.

No quiero establecer leftbarbuttonitem.

Puede alguien ayudarme en esta sencilla tarea.

Gracias,

Author: rustylepord, 2014-04-17

9 answers

Haga esto en el controlador de vista padre, no en el hijo

Swift

navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)

Objetivo-C

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
 86
Author: rustylepord,
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-07 18:43:03
self.navigationController.navigationBar.topItem.title = @"";
 32
Author: Alex_Burla,
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-07 11:20:25

Si está utilizando storyboard, puede seleccionar el elemento de navegación en el controlador de vista principal y establecer el texto del botón que desea en el campo 'Botón atrás'. Recuerde establecer esto en el controlador de vista padre, no en el hijo que se empuja.

introduzca la descripción de la imagen aquí

 7
Author: P.L.,
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-11-05 02:32:33

Prueba esta esperanza será trabajo

UIBarButtonItem *btn = 
        [[UIBarButtonItem alloc] initWithTitle:@"New Title" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:btn];
 5
Author: morroko,
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-04-17 13:01:31

Necesitaba usar self.navigationController.navigationBar.backItem.title = @"";, la diferencia es que estoy usando backItem en lugar de topItem.

 3
Author: Brian,
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-04-28 13:21:39
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
 1
Author: WoShiNiBaBa,
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-04-26 13:40:17

Swift 2.0:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.topItem?.title = ""
}

Nota: Solo funciona si storyboard tiene una cadena de pila de navegación.

Otras opciones / cambio de título:

self.navigationController?.navigationBar.backItem?.title = ""
navigationItem.backBarButtonItem?.title = ""
navigationItem.leftBarButtonItem?.title = ""

Eliminando navigationItem:

navigationItem.setLeftBarButtonItem(nil, animated: true)
 1
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
2018-07-02 15:54:12

En AppDelegate en el DidFinishLaunchingWithOptions añadir este código:

[[UIBarButtonItem appearance] 
setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000.0, 0.0) 
forBarMetrics:UIBarMetricsDefault];
 0
Author: Gaurav Malhotra,
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-07-11 19:02:37

Para eliminar el título del botón atrás para todos los controladores de vista, agregue un nuevo archivo swift y copie esta extinción

import UIKit

extension UIViewController {
    static func swizzle(){


        let orginalSelector = #selector(viewDidLoad)
        let swizzledSelector = #selector(swizzledViewDidLoad)

        let orginalMethod = class_getInstanceMethod(UIViewController.self, orginalSelector)
        let swizzledMethod = class_getInstanceMethod(UIViewController.self, #selector(swizzledViewDidLoad))

        let didAddMethod = class_addMethod(UIViewController.self, orginalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))

        if didAddMethod {
            class_replaceMethod(UIViewController.self, swizzledSelector, method_getImplementation(orginalMethod!), method_getTypeEncoding(orginalMethod!))
        }else{
            method_exchangeImplementations(orginalMethod!, swizzledMethod!)
        }

    }

    @objc func swizzledViewDidLoad(){
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        swizzledViewDidLoad()
    }


}

Después de eso en AppDelegate inside didFinishLaunchingWithOptions, llame al func swizzle.

UIViewController.swizzle()

Este func utiliza el tiempo de ejecución de objective c para intercambiar el método viewDidLoad con otro que elimina el título del botón atrás y luego en su interior recupera el original viewDidLoad

 -1
Author: Abedalkareem Omreyh,
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-11-20 14:00:05