Ios 导航栏按钮不存在';无法在导航栏中显示

Ios 导航栏按钮不存在';无法在导航栏中显示,ios,swift,uinavigationbar,uibarbuttonitem,Ios,Swift,Uinavigationbar,Uibarbuttonitem,我通过编程方式将rightBarButton设置在navigationBar上,但是没有显示,尽管标题显示得很好 这是我的代码表AppDelegate: UINavigationBar.appearance().barTintColor = APP_COLOR UINavigationBar.appearance().tintColor = UIColor.white UINavigationBar.appearance().titleT

我通过编程方式将
rightBarButton
设置在
navigationBar
上,但是没有显示,尽管标题显示得很好

这是我的代码表
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
}
我还尝试删除外观设置,但按钮仍然不可见


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

override func viewDidLoad() {
    super.viewDidLoad()

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

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

override func viewDidLoad() {
    super.viewDidLoad()

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

不要使用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
}

希望您的问题能通过此代码得到解决。谢谢。

不要使用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
}
     let rightBarButton = UIBarButtonItem(image: UIImage(named: "test"), style: .plain, target: self, action: #selector(logoutButtonTapped))

    self.navigationController?.navigationBar.topItem?.rightBarButtonItem = rightBarButton
希望您的问题能通过此代码得到解决。谢谢

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

    self.navigationController?.navigationBar.topItem?.rightBarButtonItem = rightBarButton
这对我来说很有用



现在这对我有用了

您在哪里设置此屏幕的标题(Naeem)?请尝试打印self.navigationController,查看其nil@Adeel在ViewWillDisplay中,我从数据库中获取数据,然后将名称设置为navigationTitle@AgentChocks. 不,不是零brother@Adeel当我从rootViewController进入此控制器时,它不会显示,但当我从其他控制器(不是rootViewController)进入此控制器时,它会显示您在哪里设置此屏幕的标题(Naeem)?尝试打印self.navigationController,查看其nil@Adeel在视图中将出现,我从数据库中获取数据,然后将名称设置为navigationTitle@AgentChocks. 不,不是零brother@Adeel当我从rootViewController启动此控制器时,它不显示,但当我从其他控制器启动此控制器时,它不显示,但当我从rootViewController启动此控制器时,它显示直到不显示,当我从rootViewController启动此控制器时,它不显示,当你从另一个控制器来的时候,这显示了。你是怎么来到这个控制器的<代码>窗口?.rootViewController=UINavigationController(rootViewController:ViewController())您是否使用了此代码?让我看看你调用此控制器的代码,这样我就可以知道你是如何到达此控制器的。如果你没有使用此
窗口?。rootViewController=UINavigationController(rootViewController:ViewController())
,那么你将不会在视图中看到navigationBar。如果您来自某个控制器,那么应该使用
self.navigationController?.pushViewController(ViewController(),动画:true)
。如果您没有此代码,则无法获得navigationBar.let chatHistoryVC=self.storyboard?.InstanceEviewController(标识符为:“chatHistoryVC”)作为!ChatHistoryVC self.navigationController?.SetViewController([ChatHistoryVC],动画:true)
let storyBoard:UIStoryboard=UIStoryboard(名称:“storyBoard”,bundle:nil)let programSchedule=storyBoard.InstanceViewController(标识符为:“SessionFeedbackController”)as!SessionFeedbackController self.navigationController?.pushViewController(programSchedule,动画:false)
试试这段代码,我认为它应该适合你。仍然没有显示,当我从根viewController进入这个控制器时,这个没有显示,当我从其他控制器进入时,这个显示。你是如何进入这个控制器的<代码>窗口?.rootViewController=UINavigationController(rootViewController:ViewController())您是否使用了此代码?让我看看你调用此控制器的代码,这样我就可以知道你是如何到达此控制器的。如果你没有使用此
窗口?。rootViewController=UINavigationController(rootViewController:ViewController())
,那么你将不会在视图中看到navigationBar。如果您来自某个控制器,那么应该使用
self.navigationController?.pushViewController(ViewController(),动画:true)
。如果您没有此代码,则无法获得navigationBar.let chatHistoryVC=self.storyboard?.InstanceEviewController(标识符为:“chatHistoryVC”)作为!ChatHistoryVC self.navigationController?.SetViewController([ChatHistoryVC],动画:true)
let storyBoard:UIStoryboard=UIStoryboard(名称:“storyBoard”,bundle:nil)let programSchedule=storyBoard.InstanceViewController(标识符为:“SessionFeedbackController”)as!SessionFeedbackController self.navigationController?.pushViewController(programSchedule,动画:false)
试试这段代码,我认为它应该适合你。仍然不显示,当我从根viewController进入这个控制器时,这个不显示,当来自其他控制器时,这个显示。仍然不显示,当我从根视图控制器进入该控制器时,则不显示,当我从其他控制器进入时,则显示。