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_915918415 小时前
Wireshark、Fiddler、Charles抓包工具详细使用指南
android·ios·小程序·https·uni-app·iphone·webview
TheNextByte117 小时前
如何将文件从Android无线传输到 iPad
android·ios·ipad
2501_9151063219 小时前
如何在iPad上高效管理本地文件的完整指南
android·ios·小程序·uni-app·iphone·webview·ipad
2501_9151063220 小时前
iOS 成品包加固,在只有 IPA 的情况下,能做那些操作
android·ios·小程序·https·uni-app·iphone·webview
Free Tester20 小时前
在iPhone上显示点击屏幕点击的方法
功能测试·ios·iphone
AALoveTouch21 小时前
某麦APP抢票技术解析实现
android·ios
2501_915909061 天前
苹果iOS应用上架详细流程与注意事项解析
android·ios·小程序·https·uni-app·iphone·webview
莫桐1 天前
微信小程序-ios环境下webview打开的h5页面replace跳转方式不生效问题
ios·微信小程序·小程序
搜狐技术产品小编20231 天前
iOS OOM治理
macos·ios·objective-c·cocoa
2501_915909061 天前
在无需越狱的前提下如何对 iOS 设备进行文件管理与数据导出
android·macos·ios·小程序·uni-app·cocoa·iphone