I'm trying to display a custom button for the tab bar item in my Swift project. I added a png file, called btn_new, to the Assets folder of the Xcode project and tried to display the custom button in the custom tabbar controller class. But I can only see a circle button with the default blue color and no custom image on it in my simulator.
this is the custom tabbar controller class.
import UIKit
class CustomTabBarController: UITabBarController {
var createEventViewController: CreateEventViewController!
override func viewDidLoad() {
super.viewDidLoad()
createEventViewController = CreateEventViewController()
self.delegate = self
self.tabBar.barTintColor = UIColor.customGreen()
}
func createListNC() -> UINavigationController {
let listVC = listViewController()
listVC.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "btn_new"), tag: 0)
return UINavigationController(rootViewController: listVC)
}
func setUpTabbarItems() -> [UIViewController]{
return [createListNC()]
}
}
extension CustomTabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController == tabBarController.viewControllers?[0] {
let vc = CreateEventViewController()
let nc = UINavigationController(rootViewController: vc)
nc.modalPresentationStyle = .popover
self.present(nc, animated: true, completion: nil)
return false
}
return true
}
}
I double-checked the name is called exactly "btn_new", so I was not sure why the button is not displayed. Not really sure but, one thing I am concerned about is that I did not set a size for this custom icon. Can anyone tell me how can I display the button image for the tabbar item?
CodePudding user response:
Goto Storyboard-> Select tabBarItem on VC(blue selected area)
CodePudding user response:
In your CustomTabBarController viewDidload:
let buttonImage: UIImage! = UIImage(named:
"btn_new")!.withRenderingMode(.alwaysOriginal)
(tabBar.items![0] ).selectedImage = buttonImage