ios-从“返回”按钮中删除文本,保留图标

我想从“后退”按钮中删除文本,但我想保留该图标。 我努力了

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

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

lmiguelvargasf asked 2019-10-09T06:06:49Z
24个解决方案
97 votes

我知道这已经有了答案,但是您也可以通过代码来完成(以防您正在使用笔尖)

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

将以上内容添加到第一个视图控制器中。

请注意,您需要对要推送的每个视图控制器执行此操作。 因此,如果您有3个视图控制器,并且要从所有视图控制器中删除背景文本,则必须在视图控制器1和2中添加该行。

d9rad answered 2019-10-09T06:07:42Z
72 votes

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

enter image description here

m8labs answered 2019-10-09T06:07:04Z
27 votes

如果要从按下的视图控制器中删除后退按钮的标题,例如在subSettingsViewController中将<Settings更改为<,则必须在SettingsViewController的viewWillDisappear()方法中设置backBarButtonItem标题。

Objective-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)
}
Muhammad Umair answered 2019-10-09T06:08:14Z
24 votes

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

技巧是在主视图消失时清除标题,并在再次显示时重新设置标题:

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

    // 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"
}
Stefan answered 2019-10-09T06:08:49Z
20 votes

就我而言,对于自定义图标和标题,这确实可以解决问题(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)
Rafiqul Hasan answered 2019-10-09T06:09:14Z
20 votes

如果要后退箭头,则将以下代码放入AppDelegate文件中,放入didFinishLaunchingWithOptions方法中。

对于斯威夫特

let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .normal)
Pradeep Bishnoi answered 2019-10-09T06:09:46Z
15 votes

经过大量搜索后,我发现了最佳,简单的解决方案,这将影响所有用Swift 4.2编写的viewControllers

extension UIViewController {
            open override func awakeFromNib() {
                navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        }  
}
iOS Lifee answered 2019-10-09T06:10:11Z
13 votes

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

enter image description here

rmd2 answered 2019-10-09T06:10:36Z
11 votes

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

只需添加一个透明图像,然后在AppDelegate中添加以下代码即可。

UIBarButtonItem.appearance().setBackButtonBackgroundImage(#imageLiteral(resourceName: "transparent"), for: .normal, barMetrics: .default)
Lalit Kumar answered 2019-10-09T06:11:10Z
10 votes

对于Swift 4+,请在didFinishLaunchingWithOptions将这些行放入AppDelegate

let BarButtonItemAppearance = UIBarButtonItem.appearance()

BarButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)      
BarButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, 0), for:UIBarMetrics.default)
BilalReffas answered 2019-10-09T06:11:35Z
8 votes

迅速4

self.navigationController?.navigationBar.topItem?.title = ""
Amalendu Kar answered 2019-10-09T06:12:04Z
4 votes

如果您有ViewControllerA,并且要导航到ViewControllerB,请在ViewControllerA中将当前的NavigationItem设置为新的UIBarButtonItem,并将标题设置为带有空格的字符串,然后再推送至另一个视图控制器:

将标题设置为带有空格的字符串,您不能设置nil或“”(空字符串),因为默认值为nil

let backItem = UIBarButtonItem()
backItem.title = " "
navigationItem.backBarButtonItem = backItem
let controllerB = ViewControllerB()
navigationController?.pushViewController(controllerB, animated: true)
Bradley answered 2019-10-09T06:12:41Z
3 votes
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
}
Hasya answered 2019-10-09T06:13:00Z
3 votes

将此代码放入每个VC中,这将推入另一个

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
Polina Hill answered 2019-10-09T06:13:25Z
2 votes

要从导航控制器堆栈中的所有viewcontroller中删除:

子类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)
}
Gaston Gonzalez answered 2019-10-09T06:13:58Z
2 votes

有时,如果标题很长,仅更改标题颜色是行不通的。 因为它可能会将导航栏标题移到左侧。 因此,为防止发生这种情况,除了使其透明之外,您可能还需要水平移动条形按钮的标题:

let barButtonItemAppearance = UIBarButtonItem.appearance()
    barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
    barButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, 0), for:UIBarMetrics.default)
Max Niagolov answered 2019-10-09T06:14:29Z
1 votes

对我来说,这就是窍门:

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,它将似乎起作用。 但是,如果您尝试使用手势返回,然后取消并停留在推送的视图控制器上,则返回标题将返回。

在Swift 4中工作

mrc answered 2019-10-09T06:15:04Z
1 votes

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

例如,如果您要将A控制器推到B控制器,请将空格放在A控制器导航栏中。

Tushar Raikwar answered 2019-10-09T06:15:38Z
1 votes

Xcode 10,Swift 4+

与此处其他答案的答案类似,但值得注意的是,如果仍未清除文本,则必须单击空格,然后按Enter。

enter image description here

Aishat Olowoshile answered 2019-10-09T06:16:12Z
0 votes

我在Appdelegate的didFinishLaunchingWithOptions函数中使用了此代码。

if #available(iOS 11, *) {
            UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
            UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)
        } else {
            UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0,
                                                                                           -60), for:UIBarMetrics.default)
        }
Varun Naharia answered 2019-10-09T06:16:38Z
0 votes

在带有Swift的Xcode 9.2中,它的工作方式如下:

override func viewWillDisappear(_ animated: Bool) {
   super.viewWillDisappear(true)
   navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
Thiago Bueno answered 2019-10-09T06:17:03Z
0 votes

对于我们无法完全控制所有以前的视图控制器的情况(即,如果我们在框架中工作),我们可以按以下方式删除后退按钮的标题:

// 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)
}
Ángel Téllez answered 2019-10-09T06:17:42Z
0 votes

这将解决您的问题:

    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)

    }
}
mossman252 answered 2019-10-09T06:18:09Z
0 votes

我尝试了几个答案,但无法在所有情况下都能使它们起作用。 因此,这是一种不影响导航栏标题(如果已设置)的解决方法。

    guard let items = viewController.navigationController?.navigationBar.items else { return }
    for item in items {
        if item.title == nil {
            item.title = ""
        }
    }
Rikard Platus answered 2019-10-09T06:18:35Z
translate from https://stackoverflow.com:/questions/33025239/remove-text-from-back-button-keeping-the-icon