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

相关推荐
2501_915106324 小时前
如何查看手机使用记录:Android和iOS设备全面指南
android·ios·智能手机·小程序·uni-app·iphone·webview
0xAaron11 小时前
确定crash文件和dSYM是否对应
ios·uuid·crash·dsym
0xAaron13 小时前
符号表和 dSYM UUID 确认
ios·cocoa·uuid·符号表·dsym
0xAaron13 小时前
如何使用dSYM文件来符号化崩溃信息
ios·swift·调试·崩溃·符号化·dsym
2501_9159184115 小时前
Flutter 加固方案全解析,从 Dart 层到 IPA 成品的多工具协同防护体系
flutter·macos·ios·小程序·uni-app·cocoa·iphone
wsxlgg15 小时前
IOS 打包上传提示you do not have required contracts to perform an operation
ios
每周报刊15 小时前
初代 iPhone SE 谢幕:被标为 “过时”,小屏旗舰时代彻底落幕
ios·iphone
RollingPin16 小时前
iOS 动态库与静态库的区别
ios·framework·动态库·静态库·符号表·三方库·dyld
0xAaron16 小时前
ips 文件符号化
ios·swift·调试·ips·符号化
AirDroid_cn16 小时前
iPhone放大镜跟随模式下,画面抖动,如何稳定?
macos·ios·objective-c·cocoa·iphone·ipad