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

iOS 26中UIKit Toolbar显示于TabBar后方的问题求助

UIKit中Toolbar被TabBar遮挡,如何让Toolbar显示在TabBar上方?

我遇到了类似的问题:导航控制器的Toolbar显示在TabBar下方被遮挡。以下是场景代码,使用UITabBarController作为根控制器,每个Tab都包裹在UINavigationController中,所有子控制器都添加了Toolbar按钮。

截图:
Toolbar被TabBar遮挡1
Toolbar被TabBar遮挡2

核心代码:

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  var window: UIWindow?

  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = scene as? UIWindowScene else { return }

    let window = UIWindow(windowScene: windowScene)

    // Root is the tab bar controller
    let tabBarController = UITabBarController()

    // Tab 1
    let homeVC = HomeViewController()
    let homeNav = UINavigationController(rootViewController: homeVC)
    homeNav.tabBarItem = UITabBarItem(title: "Home", image: UIImage(systemName: "house"), tag: 0)

    // Tab 2
    let searchVC = SearchController()
    let searchNav = UINavigationController(rootViewController: searchVC)
    searchNav.tabBarItem = UITabBarItem(title: "Search", image: UIImage(systemName: "magnifyingglass"), tag: 1)

    // Tab 3
    let profileVC = ProfileController()
    let profileNav = UINavigationController(rootViewController: profileVC)
    profileNav.tabBarItem = UITabBarItem(title: "Profile", image: UIImage(systemName: "person.crop.circle"), tag: 2)

    tabBarController.viewControllers = [homeNav, searchNav, profileNav]

    window.rootViewController = tabBarController
    window.makeKeyAndVisible()
    self.window = window
  }
}

final class HomeViewController: ToolbarTester {
  override func viewDidLoad() {
    super.viewDidLoad()
    title = "Home"
    view.backgroundColor = .systemBackground
  }
}

final class ProfileController: ToolbarTester {
  override func viewDidLoad() {
    super.viewDidLoad()
    title = "Profile"
    view.backgroundColor = .secondarySystemBackground
  }
}

final class SearchController: ToolbarTester {
  override func viewDidLoad() {
    super.viewDidLoad()
    title = "Search"
    view.backgroundColor = .tertiarySystemBackground
  }
}

class ToolbarTester: UIViewController {
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Make sure toolbar is visible
    navigationController?.setToolbarHidden(false, animated: animated)

    let prev = UIBarButtonItem(title: "Prev", style: .plain, target: self, action: #selector(prevTapped))
    let next = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(nextTapped))
    let flex = UIBarButtonItem(systemItem: .flexibleSpace)
    setToolbarItems([prev, flex, next], animated: false)
  }

  @objc private func prevTapped() { /* ... */ }
  @objc private func nextTapped() { /* ... */ }
}

解决方案

方法1:指定Toolbar的位置为底部(iOS 16+)

在ToolbarTester的viewWillAppear方法中添加代码,明确设置Toolbar的位置为底部,让系统自动避开TabBar:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    navigationController?.setToolbarHidden(false, animated: animated)
    // 明确设置Toolbar位置
    navigationController?.setToolbarPosition(.bottom, for: .default)
    
    let prev = UIBarButtonItem(title: "Prev", style: .plain, target: self, action: #selector(prevTapped))
    let next = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(nextTapped))
    let flex = UIBarButtonItem(systemItem: .flexibleSpace)
    setToolbarItems([prev, flex, next], animated: false)
}

也可以在初始化导航控制器时全局设置:

let homeNav = UINavigationController(rootViewController: homeVC)
homeNav.toolbarPosition = .bottom

方法2:禁用Toolbar的半透明属性

半透明Toolbar会导致布局时忽略底部TabBar的高度,禁用后系统会自动调整Toolbar位置:

class ToolbarTester: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.toolbar.isTranslucent = false
    }
    
    // ... 其他代码
}

方法3:手动调整安全区域内边距

如果上述方法无效,可以手动给视图添加底部安全区域内边距,让Toolbar避开TabBar:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    navigationController?.setToolbarHidden(false, animated: animated)
    
    // 手动添加底部安全区域内边距
    if let tabBarHeight = tabBarController?.tabBar.frame.height {
        additionalSafeAreaInsets.bottom = tabBarHeight
    }
    
    let prev = UIBarButtonItem(title: "Prev", style: .plain, target: self, action: #selector(prevTapped))
    let next = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(nextTapped))
    let flex = UIBarButtonItem(systemItem: .flexibleSpace)
    setToolbarItems([prev, flex, next], animated: false)
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 00:35:01