iOS26适配指南之UITabBarController

介绍

  • 增加了类型为UITabBarController.MinimizeBehaviortabBarMinimizeBehavior属性,用于设置 Tabbar 最小化时的行为。
  • 增加了类型为UITabAccessorybottomAccessory属性,用于在 Tabbar 的上方再添加一个 UITabAccessory(辅助内容)。
  • UISearchTab 会从 TabBar 分离出来单独显示。

使用

  • 代码。
swift 复制代码
import UIKit

// MARK: - 自定义UITabBarController
class TabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()

        tabs.append(configTab(UIViewController(), title: "微信", imageName: "message", identifier: "chats", badgeValue: "3"))
        tabs.append(configTab(UIViewController(), title: "通讯录", imageName: "person.2", identifier: "contacts"))
        tabs.append(configTab(UIViewController(), title: "发现", imageName: "safari", identifier: "discover"))
        tabs.append(configTab(UIViewController(), title: "我", imageName: "person", identifier: "me"))
        tabs.append(configSearchTab(UIViewController(), title: "搜索"))
        selectedTab = tabs.last
        // iOS26新增,向下滚动时,只显示第一个与UISearchTab的图标,中间显示辅助UITabAccessory
        self.tabBarMinimizeBehavior = .onScrollDown
        // iOS26新增
        self.bottomAccessory = UITabAccessory(contentView: UIToolbar())
    }

    // MARK: 设置UITab
    func configTab(_ viewController: UIViewController,
                   title: String,
                   imageName: String,
                   identifier: String,
                   badgeValue: String? = nil) -> UITab {
        let tab = UITab(title: title, image: UIImage(systemName: imageName), identifier: identifier) { tab in
            tab.badgeValue = badgeValue
            tab.userInfo = identifier
            let scrollView = UIScrollView(frame: UIScreen.main.bounds)
            scrollView.backgroundColor = .init(red: .random(in: 0 ... 1), green: .random(in: 0 ... 1), blue: .random(in: 0 ... 1), alpha: 1.0)
            scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: 1500)
            viewController.view.addSubview(scrollView)
            return self.configViewController(viewController: viewController, title: title)
        }
        return tab
    }

    // MARK: 设置UISearchTab
    func configSearchTab(_ viewController: UIViewController, title: String) -> UISearchTab {
        // UISearchTab,从TabBar分离出来单独显示
        let searchTab = UISearchTab { tab in
            viewController.view.backgroundColor = .init(red: .random(in: 0 ... 1), green: .random(in: 0 ... 1), blue: .random(in: 0 ... 1), alpha: 1.0)
            return self.configViewController(viewController: viewController, title: title)
        }
        return searchTab
    }

    // MARK: 设置UIViewController
    func configViewController(viewController: UIViewController, title: String) -> UINavigationController {
        let navigationController = UINavigationController(rootViewController: viewController)
        viewController.navigationItem.title = title
        return navigationController
    }
}
  • 效果。
相关推荐
伯阳在成长1 小时前
SwiftUI @ViewBuilder 的魔法
swift
MaoJiu2 小时前
Flutter混合开发:在iOS工程中嵌入Flutter Module
flutter·ios
2501_915921433 小时前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916008895 小时前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview
Digitally6 小时前
如何将视频从 iPhone 转移到 Mac
macos·ios·iphone
2501_916007477 小时前
uni-app iOS 文件调试常见问题与解决方案:结合 itools、克魔、iMazing 的实战经验
android·ios·小程序·https·uni-app·iphone·webview
豆豆(设计前端)7 小时前
使用 Uni-app 打包 外链地址APK 及 iOS 注意事项
ios·uni-app
库奇噜啦呼10 小时前
属性关键字
ios
2501_9159184114 小时前
uni-app 项目 iOS 上架踩坑经验总结 从证书到审核的避坑指南
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者814 小时前
iOS 上架 uni-app 流程全解析,从打包到发布的完整实践
android·ios·小程序·https·uni-app·iphone·webview