ios 从后退按钮中删除文本保留图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33025239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:56:39  来源:igfitidea点击:

Remove text from Back button keeping the icon

iosswiftxcode

提问by lmiguelvargasf

I want to remove the text from the back button, but I want to keep the icon. I have tried

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

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

However, this removes completely the text and the icon.

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

回答by d9rad

I know this already has an answer, but you can also do it in code (in case you're working with nibs)

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

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

Add the above in the first view controller.

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

Note that you need to do this for each view controller that is pushing. So if you have 3 view controllers, and you want to remove the back text from all of them, you'll have to add the line in view controller 1 and 2.

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

回答by m8labs

The method of @rmd2 is almost right, but instead you should select the navigation bar of the controller to which back button will point to and type " "in the Back Button field.

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

enter image description here

在此处输入图片说明

回答by iOS Lifee

After Searching a lot I found best and simple solution, this will affect all the viewControllers written in Swift 4.2 and also working in Swift 5

经过大量搜索,我找到了最好和最简单的解决方案,这将影响所有用 Swift 4.2 编写的 viewControllers并且也在 Swift 5 中工作

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

回答by Muhammad Umair

If you wanted to remove the title of a back button from a pushed view controller, let's say <Settingsto <in subSettingsViewController then you've to set backBarButtonItemtitle in SettingsViewController's viewWillDisappear()method.

如果你想从一个推视图控制器删除后退按钮的标题,比方说<Settings<在subSettingsViewController,那么你已经设置backBarButtonItem冠军SettingsViewController的viewWillDisappear()方法。

Objective-C:

目标-C:

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

Swift:

迅速:

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

回答by Stefan

The text of the back button depends on the title of the master view.

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

The trick is to clear the title if the master view disappears and set it again if it is shown again:

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

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"
}

回答by Pradeep Bishnoi

If you want back arrow so following code put into AppDelegate file into didFinishLaunchingWithOptions method.

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

For Swift

对于斯威夫特

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

回答by Rafiqul Hasan

In my case, for custom icon and title this did the trick (Swift 4)

在我的情况下,对于自定义图标和标题,这可以解决问题(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)

回答by rmd2

I solved this problem by adding a " " on the StoryBoard Title of the previous ViewController. Just a space, not empty ;D

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

enter image description here

在此处输入图片说明

回答by Lalit Kumar

Finally found perfect solution.

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

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

只需添加一个透明图像并在您的 AppDelegate 中添加以下代码。

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

回答by Amalendu Kar

in swift 4

在 迅速 4

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