I'm building a film catalog app. I have a problem with the UI. When I use a tabbar, a space appears on the top of my screen.
How can I fix this problem?
import UIKit
class TabbarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.setHidesBackButton(true, animated: false)
let vc1 = ListMovieViewController()
let vc2 = SearchViewController()
vc1.title = "List"
vc2.title = "Search"
vc1.navigationItem.largeTitleDisplayMode = .always
vc2.navigationItem.largeTitleDisplayMode = .always
let nav1 = UINavigationController(rootViewController: vc1)
let nav2 = UINavigationController(rootViewController: vc2)
nav1.tabBarItem = UITabBarItem(title: "List", image: UIImage(systemName: "film"), tag: 1)
nav2.tabBarItem = UITabBarItem(title: "Search", image: UIImage(systemName: "magnifyingglass"), tag: 1)
nav1.navigationBar.prefersLargeTitles = true
nav2.navigationBar.prefersLargeTitles = true
setViewControllers([nav1,nav2], animated: false)
}
}
import UIKit
class ListMovieViewController: UIViewController {
private let tableView: UITableView = {
let tableVw = UITableView()
tableVw.register(FilmCell.self, forCellReuseIdentifier: "FilmCell")
return tableVw
}()
private var filmViewModels: FilmViewModels = FilmViewModels()
override func viewDidLoad() {
super.viewDidLoad()
//NavBar
title = "Catalog"
view.backgroundColor = .systemBackground
navigationController?.navigationBar.prefersLargeTitles = true
SetUp()
}
private func SetUp(){
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
tableView.frame = view.bounds
}
}
extension ListMovieViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filmViewModels.getLength()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FilmCell", for: indexPath) as! FilmCell
let film = filmViewModels.films[indexPath.row]
let imageUrl = film.imageUrl
cell.filmImageView.downloaded(from: imageUrl)
cell.titleLabel.text = film.title
cell.directorLable.text = "Director by \(film.director)"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 130
}
}
If you set up your TabBars like this in SceneDelegate, I think the problem will be resolved.
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
// Create the main window
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
window?.rootViewController = createTabBar()
window?.makeKeyAndVisible()
}
func createTabBar() -> UITabBarController {
let tabBar = UITabBarController()
UITabBar.appearance().tintColor = .label
tabBar.viewControllers = [createFirstVC(), createSecondVC()]
return tabBar
}
func createFirstVC() -> UINavigationController {
let firstVC = FirstViewController()
firstVC.title = "List"
firstVC.tabBarItem = UITabBarItem(title: "List", image: UIImage(systemName: "film"), tag: 0)
UINavigationBar.appearance().backgroundColor = .systemBackground
return UINavigationController(rootViewController: firstVC)
}
func createSecondVC() -> UINavigationController {
let secondVC = SecondViewController()
secondVC.title = "Search"
secondVC.tabBarItem = UITabBarItem(title: "Search", image: UIImage(systemName: "magnifyingglass"), tag: 1)
return UINavigationController(rootViewController: secondVC)
}
}