I am using native UITabBarController and my requirement is to show the title of TabBar item only on selection and rest of titles will be disappear?
In each of your viewControllers you can call a function in viewDidLoad that does this
public func removeTitle(){
if let tab = self.tabBarController?.tabBar.items, let currentTab = self.tabBarController?.selectedViewController {
tab.forEach {
if currentTab != self {
$0.title = ""
}
}
}
}
}
Then in your viewDidLoad:
viewDidLoad(){
super.viewDidLoad()
removeTitle()
}
Slide modification to @Michael Wells answers
public func removeTitle(){
if let tab = self.tabBarController?.tabBar.items {
for (index, element) in tab.enumerated() {
if index == 0{
element.title = "Home"
}else{
element.title = ""
}
}
}
}
and instead of calling it on viewDidLoad you should call it on viewDidAppear
override func viewDidAppear(_ animated: Bool) {
removeTitle()
}
here we are using index of bottom navigation {0,1,2} = {Home,Notices,Profile}
Create a custom Tabbar class like:
class CustomTabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Setbackground Color
self.tabBar.barTintColor = _WHITE_COLOR
viewDidLoadOps()
}
func viewDidLoadOps() ->Void {
let tab1 = self.tabBar.items![0]
let tab2 = self.tabBar.items![1]
setTabBarItem(tab1, "normal_image_name", "selected_image_name", "your_title", 1)
setTabBarItem(tab2, "normal_image_name", "selected_image_name", "your_title", 2)
//For normal state, the color is clear color, so you will not see any title until your tab is selected.
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for:.normal)
//Set any color for selected state
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for:.selected)
}
func setTabBarItem(_ tabBarItem: UITabBarItem, _ noramlImageName: String, _ selectedImageName: String, _ title: String, _ tag: Int) -> Void {
tabBarItem.image = UIImage(named: noramlImageName)?.withRenderingMode(.alwaysOriginal)
tabBarItem.selectedImage = UIImage(named: selectedImageName)?.withRenderingMode(.alwaysOriginal)
tabBarItem.title = title
tabBarItem.tag = tag
}
}
Please comment if you have any questions.
Happy to help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.