答案 0 :(得分:1)
Take a subclass of TabbarController.Remember to call `<UITabBarControllerDelegate>`
func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//
tabBar.selectionIndicatorImage = returnImageofSelectetab()
}
//Method called everytime when you select tab.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if tabBarController.tabBar.selectionIndicatorImage == nil {
print("Starting point delegate: Selection indicator image is nill")
}
else {
print("Starting Point Of delegate: Selection indicator image is available")
}
//HERE i gave index where I want to set image.
if tabBarController.selectedIndex == 2 {
tabBarController.tabBar.selectionIndicatorImage = nil
}
else {
tabBarController.tabBar.selectionIndicatorImage = returnImageofSelectetab()
}
if tabBarController.tabBar.selectionIndicatorImage == nil {
print("Ending point delegate: Selection indicator image is nill")
}
else {
print("Ending Point Of delegate: Selection indicator image is available")
}
}
func returnImageofSelectetab() -> UIImage {
//HERE 'img_sel' is YOUR SELECTED IMAGE SET AS BACKGROUND OF YOUR TABBARITEM
let selTab = UIImage(named: "img_sel")?.withRenderingMode(.alwaysOriginal)
let tabSize = CGSize(width: view.frame.width / 5, height: 49)
UIGraphicsBeginImageContext(tabSize)
selTab?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height))
let reSizeImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return reSizeImage!
}
答案 1 :(得分:0)
您应该继承UITabBarController
并更改UITabBarItem
内items
数组中第三个awakeFromNib
的外观。
class CustomTabBarController: UITabBarController {
override func awakeFromNib() {
super.awakeFromNib()
guard let items = tabBar.items else {
return
}
for i in 0 ..< items.count {
item.image = image
item.selectedImage = selectedImage
[...]
}
}
}