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效果。

相关推荐
Digitally2 小时前
将 iPhone 联系人转移到 Infinix 的完整指南
ios·cocoa·iphone
imLix16 小时前
RunLoop 实现原理
前端·ios
归辞...1 天前
「iOS」————设计架构
ios·架构
i紸定i1 天前
解决html-to-image在 ios 上dom里面的图片不显示出来
前端·ios·vue·html·html-to-image
YungFan2 天前
iOS26适配指南之UIButton
ios·swift
红橙Darren2 天前
手写操作系统 - 编译链接与运行
android·ios·客户端
鹏多多.2 天前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架
麦兜*3 天前
【swift】SwiftUI动画卡顿全解:GeometryReader滥用检测与Canvas绘制替代方案
服务器·ios·swiftui·android studio·objective-c·ai编程·swift
GeniuswongAir3 天前
iOS 26 一键登录失效:三大运营商 SDK 无法正常获取手机号
ios