Main Tab Bar View Controller - kaiagaoqy/NetFlix-with-UIkit GitHub Wiki

Scene Delegate

Understanding Scene Delegate and App Delegate

We will customize all scenes and windows in this project, so at the very first, make sure you understand what windows, scenes, scene delegate and app delegate

In a word,

  • App Delegate: Be responsible for application lifecycle and setup
  • Scene Delegate: scene lifecycle events like scene creation, destruction and state restoration of a UISceneSession

App Delegate

Main Methods of App Delegate

Scene Delegate

From iOS 13 and later, sceneDelegate separates UIWindow relative methods and window variable from AppDelegate Main Methods of Scene Delegate

scene(_:willConnectTo:options:)

This is the first method called in UISceneSession life cycle. This method will create a new UIWindow, sets the root view controller, and makes this window the key window to be displayed.

        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: windowScene.coordinateSpace.bounds) // Initiate a window
        window?.windowScene = windowScene // Move this window to specific scene
        window?.rootViewController = MainTabBarViewController() // Provide the content view for the window
        window?.makeKeyAndVisible() // Shows the window infront of all the other windows and makes it the key window.

UITabBarController

Create a dimmed tab bar at bottom of the window to navigate to different windows


  1. Initiate UINavigationController instance of each navigated view controller
  2. Set up features for bar items
import UIKit

class MainTabBarViewController: UITabBarController {
    var vcArr:[UINavigationController] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let vc1 = UINavigationController(rootViewController: HomeViewController())
        let vc2 = UINavigationController(rootViewController: UpcomingViewController())
        let vc3 = UINavigationController(rootViewController: SearchViewController())
        let vc4 = UINavigationController(rootViewController: DownloadViewController())
        
        setBarItem(vc: vc1, img: UIImage(systemName: "house"), title: "Home")
        setBarItem(vc: vc2, img: UIImage(systemName: "play.circle"), title: "Coming Soon")
        setBarItem(vc: vc3, img: UIImage(systemName: "magnifyingglass"), title: "Top Search")
        setBarItem(vc: vc4, img: UIImage(systemName: "arrow.down.to.line"), title: "Downloads")
        
        setViewControllers(self.vcArr, animated: true)
    }

    /// Set up bar items and add their connected view controllers to the tab bar
    open func setBarItem(vc:UINavigationController, img: UIImage?, title:String?){
        if let barImg = img{
            vc.tabBarItem.image = barImg
        }
        vc.title = title ?? ""
        if !vcArr.contains(vc){
            self.vcArr.append(vc)
            
        }
        
        
    }

}