-1

When I redirect to a page from UINavigationController, the destination page shows the contents of the Navigation Bar with more space above than expected. How can I fix this situation?

MainTabController

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)
    } 
}

HomeScreen:

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)
    }
}

How the view looks currently: home screen page

I tried to delete the safe area in UINavigationController but I failed.

5
  • Remove this line "navigationController?.navigationBar.prefersLargeTitles = true" from the ConfigureNavigationBar() Commented Jan 25, 2024 at 7:25
  • It's default prefersLargeTitles = true. If you want to customize it, you have to create a new one on your own. Commented Jan 25, 2024 at 7:28
  • navigationController?.navigationBar.prefersLargeTitles = true When I remove it, the insert icon and search icon remain in the same position @NisarAhmad Commented Jan 25, 2024 at 7:38
  • navigationController?.navigationBar.prefersLargeTitles = true When I remove it, the insert icon and search icon remain in the same position @son Commented Jan 25, 2024 at 7:39
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Jan 26, 2024 at 17:49

1 Answer 1

-1

I forgot to remove rootViewController from UINavigationController in the code I wrote before, so extra space was added

   window?.rootViewController = UINavigationController(rootViewController: MainTabBarViewController()) //wrong
   window?.rootViewController = MainTabBarViewController() // correct
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.