如何修复屏幕顶部不需要的空间?

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

我正在构建一个电影目录应用程序。我的用户界面有问题。当我使用选项卡栏时,屏幕顶部会出现一个空格。

如何解决这个问题?

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

this is my screen

ios swift uikit uitabbarcontroller
1个回答
0
投票

如果你在SceneDelegate中像这样设置TabBars,我想问题就会解决。

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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.