IOS 17 基于UITabBarController实现首页TabBar

实现方式

创建首页MainController继承自UITabBarController就可以实现Tabbar效果。

实现效果

创建几个子控制器

创建子控制器 DiscoveryController,VideoController,MeController,FeedController,RoomController,继承自BaseLogicController;BaseLogicController封装 查看文章 IOS 11 通用Base控制器封装

Swift 复制代码
//
//  DiscoveryController.swift
//  MyCloudMusic
//
//  Created by jin on 2024/8/27.
//

import UIKit

class DiscoveryController: BaseLogicController {

   
}

其余自控制器实现同DiscoveryController。

创建MainController

MainController继承自UITabBarController

Swift 复制代码
//
//  MainController.swift
//  主界面
//
//  Created by jin on 2024/8/20.
//

import UIKit

class MainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //选中高亮颜色
        tabBar.tintColor = .primaryColor
        tabBar.isTranslucent = true
        
        //添加子控制器
        addChildController(DiscoveryController(), R.string.localizable.discovery(), "Discovery")
        addChildController(VideoController(), R.string.localizable.video(), "Video")
        addChildController(MeController(), R.string.localizable.me(), "Me")
        addChildController(FeedController(), R.string.localizable.feed(), "Feed")
        addChildController(RoomController(), R.string.localizable.live(), "Live")
    }
    
    /// 添加子控制器
    func addChildController(_ target:UIViewController,_ title:String,_ imageName:String) {
        //标题
        target.tabBarItem.title = title
        
        //默认图片
        target.tabBarItem.image = UIImage(named: imageName)
        
        //选择后图片
        target.tabBarItem.selectedImage = UIImage(named: "\(imageName)Selected")
        
        //选择后文本颜色
        target.tabBarItem.setBadgeTextAttributes([.foregroundColor:UIColor.colorPrimary], for: .selected)
        
        addChild(target)
    }
}

编译能够正常显示首页Tabbar效果。

相关推荐
用户091 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan1 天前
iOS26适配指南之UIColor
ios·swift
权咚2 天前
阿权的开发经验小集
git·ios·xcode
用户092 天前
TipKit与CloudKit同步完全指南
ios·swift
法的空间2 天前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张2 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
Magnetic_h2 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
00后程序员张2 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
前端小超超2 天前
capacitor配置ios应用图标不同尺寸
ios·蓝桥杯·cocoa