Swift 从 UITabBarController 移除 space 的导航栏

Swift remove space of Navigation Bar from UITabBarController

我正在制作一个 iOS 应用程序,以编程方式创建 UI,到目前为止,我正在尝试做的是在 UITabBarController,我正在尝试使地图使用 iPhone X(及以上)中缺口下方的完整 space,但它在其下方,就好像 space 的标题一样=16=] 在那里。

即使我尝试删除 titleView 它仍然存在。

TabBarViewController

import UIKit

class TabBarViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .systemBackground
        UITabBar.appearance().barTintColor = .systemBackground
        tabBar.tintColor = .label
        setupVCs()
    }
    
    func setupVCs() {
        viewControllers = [
            createNavController(for: MapViewController(), title: NSLocalizedString("Search", comment: ""), image: UIImage(systemName: "magnifyingglass")!),
            createNavController(for: ViewController(), title: NSLocalizedString("Home", comment: ""), image: UIImage(systemName: "house")!),
            createNavController(for: ViewController(), title: NSLocalizedString("Profile", comment: ""), image: UIImage(systemName: "person")!)
        ]
    }
    
    fileprivate func createNavController(for rootViewController: UIViewController, title: String, image: UIImage) -> UIViewController {
        
        let navController = UINavigationController(rootViewController: rootViewController)
        navController.tabBarItem.title = title
        navController.tabBarItem.image = image
        //rootViewController.navigationItem.title = title
        rootViewController.navigationController?.isToolbarHidden = true
        navigationItem.titleView?.removeFromSuperview()
        navigationItem.title?.removeAll()

        return navController
    }
}

地图视图控制器

import UIKit
import MapKit

class MapViewController: UIViewController {
    var mapView: MKMapView!
    let annotation = MKPointAnnotation()
    
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.setToolbarHidden(true, animated: false)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black
        
        mapView = MKMapView()
        mapView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(mapView)
        setupConstraints()
    }
    
    fileprivate func setupConstraints() {
        let mapViewLeadingAnchor = mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
        let mapViewTrailingAnchor = mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        let mapViewTopAnchor = mapView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
        let mapViewBottomAnchor = mapView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
        
        NSLayoutConstraint.activate([mapViewLeadingAnchor, mapViewTrailingAnchor, mapViewTopAnchor, mapViewBottomAnchor])
    }
}

这是设置标题和不设置标题时地图的样子,我只是设置了标题,看看是不是因为这个。

如何删除缺口下方多余的 space?

所以基本上你需要在这里做两件事才能隐藏地图选项卡中的导航栏:

  • viewWillAppearviewDidLoad 中的 MapViewController 中隐藏导航栏:
navigationController?.setNavigationBarHidden(true, animated: false)
  • 将您的顶级约束从 safeArea 更改为视图本身:
let mapViewTopAnchor = mapView.topAnchor.constraint(equalTo: view.topAnchor)