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.
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