Swift 从 UITabBarController TabBarViewController地图视图控制器

问题描述

我正在制作一个 iOS 应用程序,以编程方式创建 UI,到目前为止我想要做的是在 MapKit 的一个选项卡内显示一个带有 UITabBarController 的地图,我正在尝试使地图使用 iPhone X(及更高版本)缺口下方的全部空间,但它位于缺口下方,就好像 NavigationController 的标题在那里一样。

即使我尝试删除 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",image: UIImage(systemName: "house")!),title: NSLocalizedString("Profile",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])
    }
}

这是设置标题时地图的样子,没有设置标题,我只是设置标题,看看是否是由于这个原因。

如何去除缺口下方的多余空间?

enter image description here

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)