Due to company regulations, I cannot upload engineering code on the company network,So I posted all the key code, it's a very small demo with only a few dozen lines of code。
SceneDelegate.swift:
let tabbar = TabBarController()
let vc0 = UIViewController()
vc0.view.backgroundColor = .red
vc0.tabBarItem = UITabBarItem(title: "第一个", image: UIImage(systemName: "square.and.arrow.up.badge.clock"), tag: 100)
let vc1 = UIViewController()
vc1.view.backgroundColor = .green
vc1.tabBarItem = UITabBarItem(title: "第二个", image: UIImage(systemName: "tray.full.fill"), tag: 101)
let vc2 = UIViewController()
vc2.view.backgroundColor = .blue
vc2.tabBarItem = UITabBarItem(title: "第三个", image: UIImage(systemName: "list.bullet.rectangle.fill"), tag: 102)
tabbar.setViewControllers([vc0,vc1,vc2], animated: true)
window?.rootViewController = tabbar
guard let _ = (scene as? UIWindowScene) else { return }
}
TabBarController.swift :
import UIKit
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setValue(CustomTabBar(), forKey: "tabBar")
if #available(iOS 18.0, *) {
self.mode = .tabBar
self.traitOverrides.horizontalSizeClass = .compact
}
}
}
class CustomTabBar: UITabBar {
var leftView = UIView() //info view
var rightView = UIView() // info view
override func sizeThatFits(_ size: CGSize) -> CGSize {
var sizeThatFits = super.sizeThatFits(size)
sizeThatFits.height = 60
return sizeThatFits
}
override func layoutSubviews() {
super.layoutSubviews()
self.backgroundColor = .gray
addSubview(leftView)
addSubview(rightView)
leftView.backgroundColor = .green
leftView.frame = CGRect(x: 10, y: 4, width: 80, height: 40)
rightView.backgroundColor = .blue
rightView.frame = CGRect(x: UIScreen.main.bounds.width - 90, y: 4, width: 80, height: 40)
let tabBarButtonW = 100.0
var tabBarButtonX = Double(UIScreen.main.bounds.width)/2 - 150
var tabBarButtonIndex = 0
for tempView in subviews {
if NSStringFromClass(type(of: tempView)) == "UITabBarButton" {
tempView.frame = CGRect(x: tabBarButtonX, y: 0, width: tabBarButtonW, height: 44)
tabBarButtonX = tabBarButtonX + tabBarButtonW + 50
tabBarButtonIndex += 1
}
}
}
}