【问题标题】:Navigation bar button doesn't show in navigationbar导航栏按钮不显示在导航栏中
【发布时间】:2019-04-17 12:17:20
【问题描述】:

我以编程方式在navigationBar 上设置了rightBarButton,但没有显示,尽管标题显示正常。

这是我的代码表单AppDelegate

    UINavigationBar.appearance().barTintColor        = APP_COLOR
    UINavigationBar.appearance().tintColor           = UIColor.white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.font:Constants.APP_REGULAR_FONT_WITH_SIZE(size: 18), NSAttributedString.Key.foregroundColor : UIColor.white]

这是来自我的viewController 的代码,我正在尝试添加rightBarButton

override func viewDidLoad()
{
    super.viewDidLoad()

    let rightBarButton = UIBarButtonItem(image: UIImage(named: "logout"), style: .plain, target: self, action: #selector(logoutButtonTapped))
    self.navigationController?.navigationItem.rightBarButtonItem = rightBarButton
}

我也尝试过删除外观设置,但按钮仍然不可见。

【问题讨论】:

  • 你在哪里设置这个屏幕的标题(Naeem)?
  • 尝试打印 self.navigationController 看看它是否为 nil
  • @Adeel 在 vi​​ewWillAppear 中,我从数据库中获取数据,然后将名称设置为 navigationTitle
  • @AgentChocks。不,它不是零兄弟
  • @Adeel 当我从 rootViewController 进入这个控制器时它没有显示,但是当我从其他控制器而不是 rootViewController 来到这里时它显示

标签: ios swift uinavigationbar uibarbuttonitem


【解决方案1】:

您可以在代码中删除self.navigationController?.

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(logoutButtonTapped))
}

【讨论】:

  • 仍然没有显示,当我从根 viewController 进入这个控制器时,这个没有显示,当来自其他控制器时,这个显示。
【解决方案2】:

不要使用 self.navigationController?.navi... 而不是使用 self.navigationItem.rightBarButton...

override func viewDidLoad()
{
    super.viewDidLoad()

    let rightBarButton = UIBarButtonItem(image: UIImage(named: "logout"), style: .plain, target: self, action: #selector(logoutButtonTapped))
    self.navigationItem.rightBarButtonItem = rightBarButton
}

希望此代码能解决您的问题。谢谢。

【讨论】:

  • 仍然没有显示,当我从根 viewController 进入这个控制器时,这个没有显示,当来自其他控制器时,这个显示。
  • 你是怎么来这个控制器的? window?.rootViewController = UINavigationController(rootViewController: ViewController())你有没有使用这个代码?让我看看你调用这个控制器的代码,这样我就可以知道你是如何到达这个控制器的。
  • 如果您没有使用此window?.rootViewController = UINavigationController(rootViewController: ViewController()),那么您将不会在视图中看到导航栏。如果你来自某个控制器,那么你应该使用self.navigationController?.pushViewController(ViewController(), animated: true)。如果您没有此代码,那么您将无法获得导航栏。
  • 让 chatHistoryVC = self.storyboard?.instantiateViewController(withIdentifier: "ChatHistoryVC") 作为! ChatHistoryVC self.navigationController?.setViewControllers([chatHistoryVC], animated: true)
  • let storyBoard: UIStoryboard = UIStoryboard(name: "Storyboard", bundle: nil) let programSchedule = storyBoard.instantiateViewController(withIdentifier: "SessionFeedbackController") as! SessionFeedbackController self.navigationController?.pushViewController(programSchedule, animated: false) 试试这个代码,我认为它应该适合你。
【解决方案3】:
     let rightBarButton = UIBarButtonItem(image: UIImage(named: "test"), style: .plain, target: self, action: #selector(logoutButtonTapped))

    self.navigationController?.navigationBar.topItem?.rightBarButtonItem = rightBarButton

现在这对我有用

【讨论】:

    猜你喜欢
    • 2013-01-20
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多