ios Swift - 如何隐藏导航项中的后退按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27373812/
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 03:57:36  来源:igfitidea点击:

Swift - How to hide back button in navigation item?

iosswiftxcode6

提问by Mohammad Nurdin

Right now I have two view controllers. My problem is I don't know how to hide back button after change into second view controller. Mostly references that I found in objective-C. How I code it in swift?

现在我有两个视图控制器。我的问题是我不知道如何在更改为第二个视图控制器后隐藏后退按钮。主要是我在 Objective-C 中找到的参考资料。我如何快速编码?

Hide back button code in objective-c

在objective-c中隐藏后退按钮代码

[self.navigationItem setHidesBackButton:YES animated:YES];

回答by Paulw11

According to the documentationfor UINavigationItem:

根据该文件UINavigationItem

self.navigationItem.setHidesBackButton(true, animated: true);

回答by Bruno Cunha

In case you're using a UITabBarController:

如果您使用的是 UITabBarController:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.tabBarController?.navigationItem.hidesBackButton = true
}

回答by Marwen Doukh - Maru?n Duch

Swift

迅速

// remove left buttons (in case you added some)
 self.navigationItem.leftBarButtonItems = []
// hide the default back buttons
 self.navigationItem.hidesBackButton = true

回答by Amiru Homushi

This is also found in the UINavigationController class documentation:

这也可以在 UINavigationController 类文档中找到:

navigationItem.hidesBackButton = true

回答by Harjeet Singh

navigationItem.hidesBackButton = true . put it in the viewDidLoadMethod

navigationItem.hidesBackButton = true 。把它放在 viewDidLoadMethod 中

回答by Dilip Jangid

You may try with the below code

您可以尝试使用以下代码

override func viewDidAppear(_ animated: Bool) {
    self.navigationController?.isNavigationBarHidden = true
}

回答by Stoyan

Here is a version of the answer in

这是答案的一个版本

Swift 5

斯威夫特 5

您可以从故事板中使用它:

// MARK: - Hiding Back Button

extension UINavigationItem {

    /// A Boolean value that determines whether the back button is hidden.
    ///
    /// When set to `true`, the back button is hidden when this navigation item
    /// is the top item. This is true regardless of the value in the
    /// `leftItemsSupplementBackButton` property. When set to `false`, the back button
    /// is shown if it is still present. (It can be replaced by values in either
    /// the `leftBarButtonItem` or `leftBarButtonItems` properties.) The default value is `false`.
    @IBInspectable var hideBackButton: Bool {
        get { hidesBackButton }
        set { hidesBackButton = newValue }
    }
}

Every navigation item of a view controller will have this new property in the top section of attributes inspector

视图控制器的每个导航项都将在属性检查器的顶部具有此新属性

回答by Matan

That worked for me in Swift 5 like a charm, just add it to your viewDidLoad()

这在 Swift 5 中对我很有用,只需将它添加到您的 viewDidLoad()

self.navigationItem.setHidesBackButton(true, animated: true)

回答by neeraj sachdeva

enter image description here

在此处输入图片说明

Go to attributes inspector and uncheck show Navigation Bar to hide back button.

转到属性检查器并取消选中显示导航栏以隐藏后退按钮。