从“后退”按钮中删除文本,保持图标不变

我想从后退按钮删除文本,但我想保留图标。我已经试过了

let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton

但是,这将完全删除文本和图标。

116883 次浏览

我通过在前一个 ViewController 的 StoryBoard 标题上添加一个“”来解决这个问题。只是一个空间,不是空的;

enter image description here

@ rmd2的方法几乎是正确的,但是您应该选择控制器的导航栏,后退按钮将指向它,并在 Back Button 字段中键入 " "

enter image description here

我知道这已经有了答案,但是您也可以用代码来完成(如果您使用的是 nibs 的话)

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

在第一个视图控制器中添加上述内容。

注意,您需要为每个正在推送的视图控制器执行此操作。因此,如果有3个视图控制器,并且希望从所有视图控制器中删除返回文本,则必须在视图控制器1和2中添加这一行。

let button: UIButton = UIButton (type: UIButtonType.Custom)
button.setImage(UIImage(named: "imageName"), forState: UIControlState.Normal)
button.addTarget(self, action: "backButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem(customView: button)


self.navigationItem.leftBarButtonItem = barButton


func backButtonPressed(btn : UIButton) {


// Your code
}

如果您想从按下的视图控制器中删除后退按钮的标题..。

let's say a class named 设置 is going to push SubSettingsVC then in the subSettingsVC back button will show a title <Settings so in order to remove the "settings" text from back button and make it something like <

you've to set 项目 title in SettingsVC's ViewWillDisaper () 方法。

目标 C:

- (void)viewWillDisappear:(BOOL)animated
[super viewWillDisappear:animated];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:self.navigationItem.backBarButtonItem.style target:nil action:nil];
}

斯威夫特:

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}

后退按钮的文本取决于主视图的标题。

诀窍是,如果主视图消失,清除标题,如果再次显示,则重新设置标题:

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)


// needed to clear the text in the back navigation:
self.navigationItem.title = " "
}


override func viewWillAppear(_ animated: Bool) {
   

super.viewWillAppear(animated)
self.navigationItem.title = "My Title"
}

如果您希望返回箭头,那么可以将下面的代码放入 Appgenerate 文件中,并放入 did FinishLaunchingWithOptions 方法中。

为了斯威夫特

let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .normal)

For me this did the trick:

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
self.navigationItem.title = " "
}


override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = "my amazing title"
navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
}

请注意,如果您只设置标题 不需要修改的 backBarButtonItem 它将 出现的工作。但是如果您试图使用一个手势和比 取消返回,并停留在推视图控制器上,返回的标题将返回。

Working in Swift 4

您应该选择控制器的导航栏 FROM,后退按钮将指向后退按钮并在后退按钮字段中键入“”。

例如,如果你把 A 控制器推到 B 控制器,在 A 控制器的导航栏中加入空格。

终于找到了完美的解决方案。

Just add one transparent Image and add following code in your AppDelegate.

UIBarButtonItem.appearance().setBackButtonBackgroundImage(#imageLiteral(resourceName: "transparent"), for: .normal, barMetrics: .default)

To remove from all viewcontrollers in a navigation controller stack:

子类 UINavigationController 并添加以下内容:

override func show(_ vc: UIViewController, sender: Any?) {
setEmptyBackButton(vc)
super.show(vc, sender: sender)
}


override func pushViewController(_ viewController: UIViewController, animated: Bool) {
setEmptyBackButton(viewController)
super.pushViewController(viewController, animated: animated)
}


func setEmptyBackButton(_ viewController: UIViewController) {
viewController.navigationItem.backBarButtonItem =
UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}

对于 Swift 4 + ,在 didFinishLaunchingWithOptions将这些线段放入 AppDelegate

let BarButtonItemAppearance = UIBarButtonItem.appearance()


BarButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
BarButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, 0), for:UIBarMetrics.default)

在我的例子中,对于自定义图标和标题,这个方法起到了作用(Swift 4)

    let imgBack = UIImage(named: "ic_back")


navigationController?.navigationBar.backIndicatorImage = imgBack
navigationController?.navigationBar.backIndicatorTransitionMaskImage = imgBack


navigationItem.leftItemsSupplementBackButton = true
navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)

如果你有一个 ViewControllerA 并且你想导航到 ViewControllerB,在 ViewControllerA 中,你应该设置当前的导航条目和一个新的 UIBarButtonItem 以及一个空格字符串的标题,然后推到另一个视图控制器:

Set the title to a string with a blank space, you can't set nil or "" (empty string) because the default value is nil

let backItem = UIBarButtonItem()
backItem.title = " "
navigationItem.backBarButtonItem = backItem
let controllerB = ViewControllerB()
navigationController?.pushViewController(controllerB, animated: true)

有时仅更改标题颜色是不起作用的,以防标题过长。因为它可能会将导航栏标题向左移动。所以为了防止这种情况发生,你可能需要将条形按钮的标题水平移动,同时让它变得透明:

let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
barButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, 0), for:UIBarMetrics.default)

在使用 Swift 的 Xcode 9.2中,它是这样工作的:

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}

如果我们不能控制前面的所有视图控制器(例如,如果我们在框架中工作) ,我们可以删除后退按钮的标题,如下所示:

// For iOS 10
navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = String()


// For iOS 11
navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = nil

它所做的就是导航到导航堆栈的最后一项并删除它的回标题。确保在视图控制器出现时保存原始视图:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
originalBackButtonTitle = navigationController?.navigationBar.items?.last?.backBarButtonItem?.title
// Delete title somewhere here...
}

然后重新分配,为了不破坏应用程序的任何部分:

override func viewWillDisappear(_ animated: Bool) {
navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = originalBackButtonTitle
super.viewWillDisappear(animated)
}

把这个代码放在每个 VC 中,它会推动另一个 VC

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

在雨燕4号

self.navigationController?.navigationBar.topItem?.title = ""

在搜索了很多我发现最好的和简单的解决方案,这将影响所有的 viewController用 Swift 4.2编写,也用 Swift 5工作

extension UIViewController {
open override func awakeFromNib() {
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}

在 iOS14之后

extension UIViewController {
open override func awakeAfter(using coder: NSCoder) -> Any? {
navigationItem.backButtonDisplayMode = .minimal // This will help us to remove text
return super.awakeAfter(using: coder)
}
}

Xcode 10 Swift 4 +

类似的答案在这里,但值得注意的是,如果文本仍然没有清除,您必须单击空格然后进入。

enter image description here

这将解决你的问题:

    import UIKit


extension UINavigationController{


func customizeBackArrow(){
let yourBackImage = UIImage(named: "icon_back_arrow")
self.navigationBar.backIndicatorImage = yourBackImage
self.navigationBar.tintColor = Common.offBlackColor
self.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
navigationItem.leftItemsSupplementBackButton = true
self.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "",
style: .plain, target: self, action: nil)


}
}

我已经试过几种答案了,但不是所有情况下都能成功。所以这是一个不影响导航栏标题(如果设置了)的解决方案。

    guard let items = viewController.navigationController?.navigationBar.items else { return }
for item in items {
if item.title == nil {
item.title = ""
}
}

细节

  • Xcode Version 10.2.1(10E1001) ,Swift 5

解决方案

1. 创建 UINavigationController 的自定义类

import UIKit


class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
}


extension NavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if viewController.navigationItemBackButtonTextIsHidden {
viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}
}

2. 添加 UIViewController 扩展

import UIKit


extension UIViewController {
@objc var navigationItemBackButtonTextIsHidden: Bool { return false }
}

3. 更新 ViewController 类

import UIKit


class ViewController: UIViewController {
override var navigationItemBackButtonTextIsHidden: Bool { return true }
}

Full sample

import UIKit


// MARK: - ViewController


class ViewController: UIViewController {


var screenCounter = 1


override func viewDidLoad() {
super.viewDidLoad()
setupNavigationItem()
}


override var navigationItemBackButtonTextIsHidden: Bool { return (screenCounter % 2) == 0 }
}


extension ViewController {


private func setupNavigationItem() {
navigationItem.title = "VC \(screenCounter)"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "push", style: .plain, target: self, action: #selector(pushBarButtonTouchedUpInside))
}


@objc func pushBarButtonTouchedUpInside(button: UIBarButtonItem) {
guard let navigationController = navigationController else { return }
let viewController = ViewController()
viewController.screenCounter = screenCounter + 1
viewController.view.backgroundColor = .white
navigationController.pushViewController(viewController, animated: true)
}
}


// MARK: - NavigationController


class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
}


extension NavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if viewController.navigationItemBackButtonTextIsHidden {
viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}
}


// MARK: - UIViewController extension


extension UIViewController {
@objc var navigationItemBackButtonTextIsHidden: Bool { return false }
}

结果

enter image description here

这对我有用

override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}

对我来说,覆盖所有 ViewControllers的一种替代方法是扩展 UINavigationController并设置 topViewControllerbackBarButtonItem

Xcode 11.2.1上的 Swift 5:

extension UINavigationController {
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)


let backButton = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
self.topViewController?.navigationItem.backBarButtonItem = backButton
}
}

最简单的编程方式,没有不必要的副作用,就是在 来源控制器的 awakeFromNib方法中用一个空项初始化 BackBarButtonItem(你正在从中导航) :

override func awakeFromNib() {
super.awakeFromNib()
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}

Note: If you initialized the back button later, like in the ViewDidLoad () method, than you would lose swipe-back functionality (swiping from the left edge to the right takes you one step back in the navigation stack).

然后,如果您想为不同的目标控制器设置不同的后退按钮文本,并且使用 segues,那么您可以在 准备(继续)方法中设置标题,如下所示:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let item = navigationItem.backBarButtonItem {
switch segue.identifier {
case "SceneOne": item.title = "Back"; break
case "SceneTwo": item.title = "Home"; break
case "SceneThree": item.title = nil; break // Use this scene's title
default: item.title = "" // No text
}
}
}

The easiest way to do this programmatically is to set backBarButtonItem from the parent view controller (a controller that calls push).

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()


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

这里更详细的 https://sarunw.com/posts/how-to-remove-text-from-uinavigationbar-back-button/

可以使用 UINavigationController的委托方法从后退按钮中删除文本。

class CustomNavigationController: UINavigationController {


override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}


}


extension CustomNavigationController: UINavigationControllerDelegate {
    

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: String(), style: .plain, target: nil, action: nil)
}
    

}

Swift 5 Version 移除后退按钮标题并设置自定义 Image ios 13.0 +

 let backButtonAppearance = UIBarButtonItemAppearance(style: .plain)
backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.clear]
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.backButtonAppearance = backButtonAppearance
navigationBarAppearance.setBackIndicatorImage(#imageLiteral(resourceName: "back"), transitionMaskImage: #imageLiteral(resourceName: "back"))
UINavigationBar.appearance().standardAppearance = navigationBarAppearance

添加这个代码在应用程序代表 did FinishLaucnh

在经历了很多挣扎之后,唯一对我有用的是:

  1. 单击 Storyboard/Xib 中的按钮

  2. 因为每个按钮都需要一个名称(即使你不想在 UI 上显示它) ,所以转到 Identityspector [ Cmd + Option + 4] ,在{ Document-Label }上为它写一个名称。

  3. 在 Attribute 检查器[ Cmd + Option + 5]中,将{ Style }从“普通”更改为“默认”,并将{ Type }更改为“ Custom”。

  4. 由于您没有在 Ui 按钮上显示任何文本,所以在 AttributeMonitor [ Cmd + Option + 5]中从您的资产设置一个{ Image }。

构建,运行,并有一个美好的一天!

全局隐藏后退按钮文本(iOS14 +) :

extension UINavigationController {
open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
navigationBar.topItem?.backButtonDisplayMode = .minimal
}
}