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
    }
}
  • 效果。
相关推荐
Digitally1 天前
无需 iTunes 将文件从 PC 传输到 iPhone
ios·iphone
1024小神1 天前
xcode 配置了AppIcon 但是不显示icon图标
ios·swiftui·swift
奶糖 肥晨1 天前
架构深度解析|基于亚马逊云科技与Swift Alliance Cloud构建高可用金融报文交换架构
科技·架构·swift
2501_915918411 天前
iOS 项目中证书管理常见的协作问题
android·ios·小程序·https·uni-app·iphone·webview
ITKEY_1 天前
iOS网页应用无地址栏无工具栏
ios
2501_915918411 天前
提升 iOS 应用安全审核通过率的一种思路,把容易被拒的点先处理
android·安全·ios·小程序·uni-app·iphone·webview
RollingPin1 天前
iOS探究使用Block方式实现一对多回调能力
ios·block·runtime·数据分发·解耦·动态绑定·一对多回调
TheNextByte11 天前
iPhone短信备份与恢复:3种最佳方法及短信备份与恢复应用
ios·iphone
2501_916008891 天前
iOS 应用发布流程中常被忽视的关键环节
android·ios·小程序·https·uni-app·iphone·webview
ForteScarlet1 天前
Kotlin 2.3.0 现已发布!又有什么好东西?
android·开发语言·后端·ios·kotlin