如何使用 Swift 以编程方式删除 UINavigationController 的空间

问题描述 投票:0回答:1

当我从 UINavigationController 重定向到页面时,目标页面显示导航栏的内容,上面的空间比预期更多。我该如何解决这种情况?

主选项卡控制器

//
//  HomeTabBarController.swift
//  MatchVocab
//
//  Created by Yasin Kabak on 13.01.2024.
//

import UIKit

final class MainTabBarViewController: UITabBarController{
    override func viewDidLoad() {
        super.viewDidLoad()
        
   
        view.backgroundColor = .white
        
        let homeVC = UINavigationController(rootViewController: HomeScreen())
        let searchVC = UINavigationController(rootViewController: SearchScreen())
        let favouriteVC = UINavigationController(rootViewController: FavouriteScreen())
        
        homeVC.tabBarItem = UITabBarItem(title: "home", image: UIImage(systemName: "house"), tag: 0)
        searchVC.tabBarItem = UITabBarItem(title: "search", image: UIImage(systemName: "magnifyingglass"), tag: 1)
        favouriteVC.tabBarItem = UITabBarItem(title: "favourite", image: UIImage(systemName: "heart"), tag: 2)
        setViewControllers([homeVC,searchVC,favouriteVC], animated: true)
        
        
        
    }
    
}

主屏幕:

import UIKit

enum Sections: Int {
    case Home, Favourite, Search
}

protocol HomeScreenInterface: AnyObject {
    func configureVC()
    func configureNavigationBar()
    func configureTableView()
}

final class HomeScreen: UIViewController {
    var viewModel = HomeViewModel()
    var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel.view = self
        viewModel.viewDidLoad()
        viewModel.fetchWordFromCoreData()
    }
}

extension HomeScreen: HomeScreenInterface {
    func configureVC() {
        view.backgroundColor = .systemBackground.withAlphaComponent(0.8)
        overrideUserInterfaceStyle = .light
    }

    func configureTableView() {
        tableView = UITableView(frame: .zero)
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(WordCell.self, forCellReuseIdentifier: WordCell.identifier)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.backgroundColor = .systemBackground.withAlphaComponent(0.8)
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        ])
    }

    func configureNavigationBar() {
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.title = "Words"
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemBlue]
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "plus"), style: .plain, target: self, action: #selector(addWord))
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "magnifyingglass"), style: .plain, target: self, action: #selector(searchWord))
    }
}

extension HomeScreen: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat.getScreenHeight() * 0.05
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.words.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: WordCell.identifier, for: indexPath) as! WordCell
        cell.setCell(model: viewModel.words[indexPath.row])
        return cell
    }
}

extension HomeScreen {
    @objc func addWord() {
        let vc = UINavigationController(rootViewController: CreateScreen())
        vc.modalPresentationStyle = .fullScreen
        present(vc, animated: true)
    }

    @objc func searchWord() {
        let vc = SearchScreen()
        vc.modalPresentationStyle = .fullScreen
        present(vc, animated: true)
    }
}

图片:

home screen page

我尝试删除 UINavigationController 中的安全区域,但失败了

ios swift mobile uiviewcontroller uinavigationbar
1个回答
0
投票

我之前写的代码中忘记从UINavigationController中删除rootViewController,所以添加了额外的空间

   window?. = UINavigationController(rootViewController: MainTabBarViewController()) //wrong
   window?.rootViewController = MainTabBarViewController() // correct
© www.soinside.com 2019 - 2024. All rights reserved.