ios 使用 Swift 中的 hidesBackButton 在导航栏中隐藏后退按钮

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

Hide back button in navigation bar with hidesBackButton in Swift

iosswiftios8uinavigationbar

提问by Amer Hukic

I want to hide the back button when transitioning from one view to another. I read the questions regarding this problem and every answer was "use hidesBackButton". The problem with this is:

从一个视图转换到另一个视图时,我想隐藏后退按钮。我阅读了有关此问题的问题,每个答案都是“使用hidesBackButton”。问题在于:

  • when I put it in viewDidLoad/viewWillAppear the back button arrow hides but the string "Back" doesn't.

  • when I put it in viewDidAppear the back button disappears but it visible to the user

  • 当我把它放在 viewDidLoad/viewWillAppear 中时,后退按钮箭头会隐藏,但字符串“后退”没有。

  • 当我把它放在 viewDidAppear 中时,后退按钮消失了,但它对用户可见

How can I fix this?

我怎样才能解决这个问题?

Edit:

编辑:

Here is how you can replicate this problem(or bug?)
Make a new Tabbed application with Swift in Xcode. In the FirstViewController.swift use performSegueWithIdentifier to navigate to the second view controller. In the SecondViewController.swift hide the navigation bar back button using hidesBackButton and you will see what the problem is.

以下是如何复制此问题(或错误?)
在 Xcode 中使用 Swift 创建一个新的选项卡式应用程序。在 FirstViewController.swift 中使用 performSegueWithIdentifier 导航到第二个视图控制器。在 SecondViewController.swift 中使用 hidesBackButton 隐藏导航栏后退按钮,您将看到问题所在。

回答by ObjectiveTC

To hide the back button with the latest Swift:

要使用最新的 Swift 隐藏后退按钮:

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

回答by ericgu

Try adding this:

尝试添加这个:

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

回答by Celil Bozkurt

You can use the code below to hide back button on UINavigationBar.

您可以使用下面的代码隐藏 上的后退按钮UINavigationBar

Swift 3;

斯威夫特 3;

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

回答by luhuiya

this worked for me

这对我有用

navigationController?.navigationBar.topItem?.hidesBackButton = true

回答by A.G

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    self.navigationController?.navigationBarHidden = false
    var button: UIButton = UIButton()
    button.setImage(UIImage(named: "person-icon.jpg"), forState: .Normal)
    button.frame = CGRectMake(0, 0, 25, 25)
    button.targetForAction("actioncall", withSender: nil)
    var rightItem:UIBarButtonItem = UIBarButtonItem()
    rightItem.customView = button
    self.navigationItem.rightBarButtonItem = rightItem

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

override func viewWillAppear(animated: Bool) {
    let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
    navigationItem.leftBarButtonItem = backButton
}

回答by Ha cong Thuan

Try adding this,This worked for me

尝试添加这个,这对我有用

navigationItem.hidesBackButton = true

回答by Bruno Cunha

This worked for me:

这对我有用:

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)

    self.tabBarController?.navigationItem.hidesBackButton = true
}

回答by Joe M

Worked for me when I set it in init(), instead of viewDidLoad. Strange though

当我在 init() 中设置它而不是 viewDidLoad 时对我有用。虽然奇怪

回答by Sir Papilonius

In XCode 11(maybe sooner, not sure), you can also untick the box under the attribute inspector tab in the storyboard editor if you're not looking to do it programatically.

在 XCode 11(也许更快,不确定)中,如果您不想以编程方式进行,也可以取消选中故事板编辑器中属性检查器选项卡下的框。