You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

嵌入UINavigationController后UITableViewController导航栏及按钮不显示问题

Hey there, let's troubleshoot why your MyPage screen isn't showing the navigation bar or its right-side button. I’ve dealt with this exact headache before, so here are the most likely fixes to walk through:

1. Verify how you're navigating to MyPage
  • If you’re using present(_:animated:completion:) to show MyPage, that’s the core problem! Presented view controllers are modal and don’t attach to the navigation stack. You need to use navigationController?.pushViewController(_:animated:) instead—this adds MyPage to the existing navigation stack, which makes the navigation bar visible.
  • Double-check your Storyboard setup too: make sure you’re either starting from the UINavigationController you embedded MyPage into (set it as the initial view controller if needed), or segueing to the navigation controller (not directly to MyPage) from your previous screen.
2. Check for accidental navigation bar hiding
  • It’s easy to accidentally hide the navigation bar somewhere. Look in your MyPage class for any line like navigationController?.isNavigationBarHidden = true—if it’s there, remove it or set it to false.
  • In Storyboard, select your UINavigationController and check the Attributes Inspector: ensure "Shows Navigation Bar" is checked. Also, select MyPage’s UITableViewController and make sure "Hide Navigation Bar on Push" is not ticked.
  • For a quick force-fix, add this to viewWillAppear(_:) to guarantee the bar shows up:
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.isNavigationBarHidden = false
}
3. Fix how you're adding the right bar button
  • Make sure you’re adding the button to the current view controller’s navigation item, not the navigation controller’s. Here’s the correct implementation in viewDidLoad():
override func viewDidLoad() {
    super.viewDidLoad()
    
    // Create the right button
    let rightBarButton = UIBarButtonItem(title: "Action", style: .plain, target: self, action: #selector(rightButtonTapped))
    // Assign it to your view controller's navigation item
    navigationItem.rightBarButtonItem = rightBarButton
    
    // Optional: Fix visibility if it's blending into the bar
    rightBarButton.tintColor = .systemBlue
}

@objc private func rightButtonTapped() {
    // Add your button action logic here
}
  • A common mistake is using navigationController?.navigationItem.rightBarButtonItem—that targets the navigation controller’s own navigation item, which isn’t what you want for your MyPage screen.
4. Confirm Storyboard connections are correct
  • Double-check that your UITableViewController in Storyboard has its Custom Class set to MyPage (remember Swift is case-sensitive, so match the spelling exactly).
  • If you’re using a segue to jump to MyPage, make sure the segue type is set to "Show" (not "Present Modally")—this triggers the push navigation that keeps the bar visible.

If none of these work, take a look at any global navigation bar settings you might have (like in AppDelegate or SceneDelegate)—sometimes those can override per-screen configurations.

内容的提问来源于stack exchange,提问作者Miloo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:50:03