介紹
- 增加了新的標(biāo)簽類型
UITab
與UITabGroup
。
- 增加了類型為 UITabBarController.Mode 的
mode
屬性,用于設(shè)置顯示模式聊替,共有 3 種取值祥楣,分別為automatic
开财、tabBar
與tabSidebar
汉柒。其中tabSidebar
在 iPadOS 中可以實現(xiàn) siderBar 與 tabBar 的相互切換。
-
UITab
可以設(shè)置title
(標(biāo)題)责鳍、subtitle
(副標(biāo)題)碾褂、image
(圖片)、badgeValue
(角標(biāo)值)等內(nèi)容历葛。
-
UITabGroup
在 iPadOS 中可以實現(xiàn)標(biāo)簽的分組與折疊正塌。
- UITabBarControllerDelegate 增加了多個與
UITab
相關(guān)的代理方法。
案例
代碼
- 自定義 UITabBarController恤溶。
import UIKit
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
tabs.append(configTab(UIViewController(), title: "微信", imageName: "message", identifier: "wechat", badgeValue: "3"))
tabs.append(configTab(UIViewController(), title: "通訊錄", imageName: "person.2", identifier: "contact"))
tabs.append(configTab(UIViewController(), title: "發(fā)現(xiàn)", imageName: "safari", identifier: "discover"))
tabs.append(configTab(UIViewController(), title: "我", imageName: "person", identifier: "me"))
tabs.append(configTabGroup(UIViewController(), title: "更多", imageName: "ellipsis", identifier: "more"))
// 選中的Tab
selectedTab = tabs.last
}
// MARK: 設(shè)置Tab
func configTab(_ viewController: UIViewController,
title: String,
imageName: String,
identifier: String,
badgeValue: String? = nil) -> UITab {
// Tab
let tab = UITab(title: title, image: UIImage(systemName: imageName), identifier: identifier) { tab in
// 角標(biāo)
tab.badgeValue = badgeValue
// 關(guān)聯(lián)對象
tab.userInfo = identifier
viewController.view.backgroundColor = .init(red: .random(in: 0 ... 1), green: .random(in: 0 ... 1), blue: .random(in: 0 ... 1), alpha: 1.0)
// 返回顯示的UIViewController
return self.configViewController(viewController: viewController, title: title)
}
// Tab內(nèi)容的顯示方式
tab.preferredPlacement = .sidebarOnly
return tab
}
// MARK: 設(shè)置UITabGroup
func configTabGroup(_ viewController: UIViewController,
title: String,
imageName: String,
identifier: String,
badgeValue: String? = nil) -> UITabGroup {
// UITabGroup
let tabGroup = UITabGroup(title: title, image: UIImage(systemName: imageName), identifier: identifier) { _ in
viewController.view.backgroundColor = .init(red: .random(in: 0 ... 1), green: .random(in: 0 ... 1), blue: .random(in: 0 ... 1), alpha: 1.0)
// 返回顯示的UIViewController
return self.configViewController(viewController: viewController, title: title)
}
// 可以添加多個Tab乓诽,siderBar時肯定會顯示,tabBar時根據(jù)Tab的preferredPlacement取值決定
tabGroup.children.append(configTab(UIViewController(), title: "設(shè)置", imageName: "gear", identifier: "setting"))
tabGroup.children.append(configTab(UIViewController(), title: "關(guān)于", imageName: "info", identifier: "about"))
return tabGroup
}
// MARK: 設(shè)置UIViewController
func configViewController(viewController: UIViewController, title: String) -> UINavigationController {
let navigationController = UINavigationController(rootViewController: viewController)
viewController.navigationItem.title = title
return navigationController
}
}
- UITabBarControllerDelegate咒程。
// MARK: - 新增代理方法
extension TabBarController: UITabBarControllerDelegate {
// MARK: Tab是否可以選中
func tabBarController(_ tabBarController: UITabBarController, shouldSelectTab tab: UITab) -> Bool {
return true
}
// MARK: 選中Tab
func tabBarController(_ tabBarController: UITabBarController, didSelectTab selectedTab: UITab, previousTab: UITab?) {
print(previousTab?.title ?? "", selectedTab.title)
}
// MARK: 開始編輯
func tabBarControllerWillBeginEditing(_ tabBarController: UITabBarController) {
print(#function)
}
// MARK: 結(jié)束編輯
func tabBarControllerDidEndEditing(_ tabBarController: UITabBarController) {
print(#function)
}
// MARK: UITabGroup中的順序發(fā)生變化
func tabBarController(_ tabBarController: UITabBarController, displayOrderDidChangeFor group: UITabGroup) {
print(#function)
}
}
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 }
window = UIWindow(windowScene: windowScene)
let tabBarController = TabBarController()
// iOS18新增,設(shè)置顯示模式
tabBarController.mode = .tabSidebar
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// iOS18新增稠集,獲取sidebar
let sidebar = tabBarController?.sidebar
}
}
效果