前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS18适配指南之UITabBarController

iOS18适配指南之UITabBarController

原创
作者头像
YungFan
发布2024-09-17 21:51:14
2500
发布2024-09-17 21:51:14
举报
文章被收录于专栏:学海无涯

介绍

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

案例

代码

  • 自定义 UITabBarController。
代码语言:swift
复制
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。
代码语言:swift
复制
// 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。
代码语言:swift
复制
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。
代码语言:swift
复制
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // iOS18新增,获取sidebar
        let sidebar = tabBarController?.sidebar
    }
}

效果

UITabBarController.gif
UITabBarController.gif

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 案例
    • 代码
      • 效果
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档