1.创建分组
2.创建自己的CustomVc作为程序的rootVc
2.1 第一步创建根视图,继承自UITabBarController
2.1.1创建一个继承UITabBarController的vc
class MainTapController:UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
}
此时初始Vc还是之前的ViewController
2.2 在SceneDelegate修改rootVc
Ios13之前是在AppDelegate中修改App的rootVc, ios13支持多屏后,需要在SceneDelegate设置rootVc,添加如下代码即可
guard let scence = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: scence)
window?.rootViewController = MainTapController()
window?.makeKeyAndVisible()
完整代码如下:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
///固定步骤
guard let scence = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: scence)
window?.rootViewController = MainTapController() //根
///Shows the window and makes it the key window.
window?.makeKeyAndVisible()
//This is a convenience method to show the current window and position it in front of all other windows at the same level or lower. If you only want to show the window, change its isHidden property to false.
}
...
}
应用程序的window是一个重要的概念。本质上,窗口就是应用程序,大多数iOS应用程序只有一个窗口。它包含应用的用户界面(UI),将事件调度到视图,并提供了一个主要背景层来显示应用内容.ios13后,window(窗口)的概念已被scene(场景)的概念所代替.
--------------------并不是太懂
3.给TabBarController 设置5个子Vc
3.1 在Controllers分组中创建再创建5个Vc如下
import UIKit
class ProfileController:UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemRed
}
}
3.2 将各个Vc做为NavVc的rootVc 并返回Nav—创建模板函数
关于NavVc和TbC的嵌套使用的原理
//template -模板
///传入nav的根Vc,和俩张不同状态的图片--返回Nav
func templateNavigationController(unselectedImage:UIImage,selectedImage:UIImage,rootViewController:UIViewController)->UINavigationController{
let nav = UINavigationController(rootViewController: rootViewController)
//tabbar是 TabbarController的属性,tabBarItem是子Vc的属性,这里应该是NavVc的rootVc的属性
nav.tabBarItem.image = unselectedImage
nav.tabBarItem.selectedImage = selectedImage
nav.navigationBar.tintColor = .black
return nav
}
3.3讲5个NavVc做为子Vc 连接TabBarController
//MARK:- Helps
///Creating an instace of controller--connected to Tabbar
func configureViewController(){
view.backgroundColor = .white
let feed = templateNavigationController(unselectedImage: #imageLiteral(resourceName: "home_unselected"), selectedImage: #imageLiteral(resourceName: "home_selected"), rootViewController: FeedController()) //like container --return navi
let search = templateNavigationController(unselectedImage: #imageLiteral(resourceName: "search_unselected"), selectedImage: #imageLiteral(resourceName: "search_selected"), rootViewController: SearchController())
let imageSelector = templateNavigationController(unselectedImage: #imageLiteral(resourceName: "plus_unselected"), selectedImage: #imageLiteral(resourceName: "plus_unselected"), rootViewController: ImageSelectController())
let notification = templateNavigationController(unselectedImage: #imageLiteral(resourceName: "like_unselected"), selectedImage: #imageLiteral(resourceName: "like_selected"), rootViewController: NotificationController())
let profile = templateNavigationController(unselectedImage: #imageLiteral(resourceName: "profile_unselected"), selectedImage: #imageLiteral(resourceName: "profile_selected"), rootViewController: ProfileController())
///An array of the root view controllers displayed by the tab bar interface.
//var viewControllers: [UIViewController]? { get set }
viewControllers = [feed,search,imageSelector,notification,profile]
//tabBar是UITabBarController的属性
tabBar.tintColor = .black
}
3.4在lifecycle加入Helper函数
//MARK:- lifecycle
//this method what gets called when the view loads in memory ---only called one time ⚠️
override func viewDidLoad() {
super.viewDidLoad()
configureViewController()
}