美文网首页iOS18适配指南iOS 工具集
iOS18适配指南之UITabBarController

iOS18适配指南之UITabBarController

作者: YungFan | 来源:发表于2024-09-16 21:50 被阅读0次

    介绍

    • 增加了新的标签类型UITabUITabGroup
    • 增加了类型为 UITabBarController.Mode 的mode属性,用于设置显示模式,共有 3 种取值,分别为automatictabBartabSidebar。其中tabSidebar在 iPadOS 中可以实现 siderBar 与 tabBar 的相互切换。
    • UITab可以设置title(标题)、subtitle(副标题)、image(图片)、badgeValue(角标值)等内容。
    • UITabGroup在 iPadOS 中可以实现标签的分组与折叠。
    • UITabBarControllerDelegate 增加了多个与UITab相关的代理方法。

    案例

    代码

    • 自定义 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: "发现", 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: 设置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
                // 角标
                tab.badgeValue = badgeValue
                // 关联对象
                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内容的显示方式
            tab.preferredPlacement = .sidebarOnly
            return tab
        }
    
        // MARK: 设置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时根据Tab的preferredPlacement取值决定
            tabGroup.children.append(configTab(UIViewController(), title: "设置", imageName: "gear", identifier: "setting"))
            tabGroup.children.append(configTab(UIViewController(), title: "关于", imageName: "info", identifier: "about"))
            return tabGroup
        }
    
        // MARK: 设置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: 结束编辑
        func tabBarControllerDidEndEditing(_ tabBarController: UITabBarController) {
            print(#function)
        }
    
        // MARK: UITabGroup中的顺序发生变化
        func tabBarController(_ tabBarController: UITabBarController, displayOrderDidChangeFor group: UITabGroup) {
            print(#function)
        }
    }
    
    • SceneDelegate。
    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新增,设置显示模式
            tabBarController.mode = .tabSidebar
            window?.rootViewController = tabBarController
            window?.makeKeyAndVisible()
        }
    }
    
    • UIViewController。
    import UIKit
    
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // iOS18新增,获取sidebar
            let sidebar = tabBarController?.sidebar
        }
    }
    

    效果

    UITabBarController.gif

    相关文章

      网友评论

        本文标题:iOS18适配指南之UITabBarController

        本文链接:https://www.haomeiwen.com/subject/vvvwljtx.html