Skip to content

Instantly share code, notes, and snippets.

@PetreVane
Created January 3, 2020 16:25
Show Gist options
  • Save PetreVane/e2de872bbfef02e1399bd70177c0b093 to your computer and use it in GitHub Desktop.
Save PetreVane/e2de872bbfef02e1399bd70177c0b093 to your computer and use it in GitHub Desktop.
This is a sample of Scene delegate methods, showing the basics of navigation in a project without StoryBoard. There are 2 ViewController files in the project: SearchBiewController & FavoritesViewController; each of them is embeded in a Navigation Controller
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 }
// init window with the frameSize of the windowScene bounds
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
//assign windowScene to window
window?.windowScene = windowScene
//assign the root ViewController
window?.rootViewController = createTabBar()
// show the ViewController
window?.makeKeyAndVisible()
}
/// Adds a Navigation controller for SearchViewController()
func createSearchNavController() -> UINavigationController {
let seachVC = SearchViewController()
seachVC.title = "Search"
seachVC.tabBarItem = UITabBarItem(tabBarSystemItem: .search, tag: 0)
return UINavigationController(rootViewController: seachVC)
}
/// Adds a Navigation controller for FavoritesViewController()
func createFavoritesNavController() -> UINavigationController {
let favoritesVC = FavoritesViewController()
favoritesVC.title = "Favorites"
favoritesVC.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 1)
let favNavController = UINavigationController(rootViewController: favoritesVC)
return favNavController
}
/// Adds a TabBar controller & assigns the Navigation controllers to it
func createTabBar() -> UITabBarController {
let tabBar = UITabBarController()
tabBar.viewControllers = [createSearchNavController(), createFavoritesNavController()]
// adding tint color for TabBar
UITabBar.appearance().tintColor = .systemTeal
return tabBar
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment