ios - 在导航 Controller 中设置后退按钮

标签 ios iphone xcode swift uinavigationcontroller

所以我有 3 个 View Controller :V1、V2 和 V3。当我按下 V1 上的按钮时,它会将我带到 V3。我想保留“后退箭头”但更改那里的文本。我已经尝试了很多事情,但是当显示 View 时,返回这个词要么收割,要么我的代码不起作用。

有什么建议吗?

我已经用过这个代码

self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "YO", style: .Plain, target: nil, action: nil)

let backbutton = UIButton(type: .Custom)
backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal) // Image can be downloaded from here below link
backbutton.setTitle("Back", forState: .Normal)
backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)

self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)

func backAction() -> Void {
    self.navigationController?.popViewControllerAnimated(true)
}

我尝试使用我在 Stack 上找到的代码创建我的 Button 但是按钮消失了,后退按钮又出现在它的位置。

最佳答案

您可以将navigationItem.leftBarButtonItem设置为您自己设计的按钮。这会覆盖 backBarButtonItem,iOS 不会用“Back”替换您的文本。但如果您想要一个箭头,则必须自己将其设计为按钮的一部分。

在我的代码中,每当标题更改时我都会设置按钮:

override var title:String? {
    get {
      return super.title
    }
    set {
      super.title = newValue
      if (navigationController?.childViewControllers.count > 1) {
        let backButton = // ... custom button
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
        backButton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)
      } else {
        navigationItem.hidesBackButton = true
      }

关于ios - 在导航 Controller 中设置后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34429702/

相关文章:

ios - 如何完全覆盖导航栏的后退按钮操作?

ios - xcode 8 w/jenkins xcode 插件,如何指定配置文件

ios - 当我点击 TextField 并且不显示键盘时。我该如何使用?

ios - 如何在不违反 AutoLayout 约束的情况下隐藏 UITableViewCells

javascript - 如何使用 Xcode/Instruments/Time Profiler 分析 React Native 源代码

ios - CGAffineTransformRotate 之后 CGRectIntersection 不起作用

iphone - iOS平移和缩放动画

iphone - UINavigationBar和可用屏幕区域的计算

ios - 无法使用 EGOTableViewPullRefresh 刷新 TableView

C++ 未知类型名称 'string'