嵌入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:
- 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 usenavigationController?.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.
- It’s easy to accidentally hide the navigation bar somewhere. Look in your
MyPageclass for any line likenavigationController?.isNavigationBarHidden = true—if it’s there, remove it or set it tofalse. - 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 }
- 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
相关产品推荐
相关产品推荐

