如何在特定选项卡栏项目下添加标题,而不是其他选项卡栏项目



我有一个标签栏控制器,项目没有文本,我按照这个答案去做

我只想将文本添加到中间的选项卡栏项目,我应该如何添加它?

class MainTabBarController: UITabBarController, UITabBarControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
delegate = self
configureTabs()
}
override func viewDidLayoutSubviews() {
var verticalOffset: CGFloat = 6.0
if #available(iOS 11.0, *), traitCollection.horizontalSizeClass == .regular {
verticalOffset = 0.0
}
let imageInset = UIEdgeInsets(
top: verticalOffset,
left: 0.0,
bottom: -verticalOffset,
right: 0.0
)
for tabBarItem in tabBar.items ?? [] {
tabBarItem.title = ""
tabBarItem.imageInsets = imageInset
}
}
fileprivate func configureTabs() {
let homeContainerVC = HomeContainerController()
let navVCZero = UINavigationController(rootViewController: homeContainerVC)
navVCZero.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "homeIcon"), tag: 0)
let discoverVC = PopularDiscoverContainerController()
let navVCOne = UINavigationController(rootViewController: discoverVC)
navVCOne.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "discoverIcon"), tag: 1)
let cameraVC = CameraController()
cameraVC.tabBarItem = UITabBarItem(title: "Post Something", image: UIImage(named: "cameraIcon"), tag: 2)
let notifcationsVC = NotifcationsController()
let navVCThree = UINavigationController(rootViewController: notifcationsVC)
navVCThree.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "notificationsIcon"), tag: 3)
let settingsVC = SettingsController()
let navVCFour = UINavigationController(rootViewController: settingsVC)
navVCFour.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "settingsIcon"), tag: 4)
viewControllers = [navVCZero, navVCOne, cameraVC, navVCThree, navVCFour]
}
//MARK:- TabBarController Delegate
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// this makes sure it's the vc you want to present modally from the tab index
if viewController is CameraController {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.clear
let cameraVC = CameraController()
let navVC = UINavigationController(rootViewController: cameraVC)
navVC.modalPresentationStyle = .fullScreen
navVC.modalPresentationCapturesStatusBarAppearance = true
tabBarController.present(navVC, animated: true, completion: nil)
// return false to have the tab present it
return false
}
// return true to go back to the tab you was initially on before you pressed it
return true
}
}

应用代表:

class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UITabBar.appearance().tintColor = UIColor.black
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont.systemFont(ofSize: 11)], for: .normal)
}
}

我得到了答案。在viewDidLayoutSubviews中,我只需要添加:

for tabBarItem in tabBar.items ?? [] {
if tabBarItem.tag != 2 {
tabBarItem.title = ""
tabBarItem.imageInsets = imageInset
}
}

最新更新