Skip to content

Instantly share code, notes, and snippets.

@vurgunmert
Created August 21, 2024 12:15
Show Gist options
  • Save vurgunmert/a634d395c01226bfbefe5b7aa462a812 to your computer and use it in GitHub Desktop.
Save vurgunmert/a634d395c01226bfbefe5b7aa462a812 to your computer and use it in GitHub Desktop.
UIKi UITabBarController Storybook tvOS
//
// UIKitUITabBarControllerStorybook.swift
// TemplateAppTvOS
//
// Created by Mert Vurgun on 21.08.2024.
//
import UIKit
class UIKitUITabBarControllerStorybook: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// MARK: 1. Set the delegate
delegate = self
// MARK: 2. Initialize View Controllers
// Each view controller is created, customized, and assigned to a tab bar item.
let homeViewController = UIKit_HomeViewController()
homeViewController.view.backgroundColor = .red
homeViewController.tabBarItem = UITabBarItem(title: "Home", image: UIImage(systemName: "house.fill"), tag: 0)
let searchViewController = UIKit_SearchViewController()
searchViewController.view.backgroundColor = .black
searchViewController.tabBarItem = UITabBarItem(title: "Search", image: UIImage(systemName: "magnifyingglass"), tag: 1)
let settingsViewController = UIKit_SettingsViewController()
settingsViewController.view.backgroundColor = .yellow
settingsViewController.tabBarItem = UITabBarItem(title: "Settings", image: UIImage(systemName: "gearshape"), tag: 2)
// MARK: 3. Assign View Controllers to the Tab Bar
// The viewControllers property of the UITabBarController is set with the array of view controllers.
viewControllers = [homeViewController, searchViewController, settingsViewController]
}
// MARK: 4. Handle Tab Bar Controller Delegate Method
// This method is called when the user selects a different tab.
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// Ensure that the currently selected view and the newly selected view are not nil.
guard let fromView = selectedViewController?.view, let toView = viewController.view else {
return false
}
// MARK: 5. Check and Animate Transition
if fromView != toView {
UIView.transition(from: fromView, to: toView, duration: 0.3, options: [.transitionCrossDissolve], completion: nil)
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment