I am working on application that is more likely a tabBarController
application. But I can not use the tabBarController
because I need a custom ta
Below code works perfectly in my project.
i have used swift3 version as below :
i added MyTabBar.xib file which contains UIView with 4 buttons.
In xib file, Set Class of UIView. class = "MyTabBar"
Give 4 buttons Tag 1,2,3,4 accordingly..
and below myTabBarController file
myTabBarController.swift code as below :
class myTabBarController: UITabBarController {
var tabBarView: UIView!
var btn1: UIButton!
var btn2: UIButton!
var btn3: UIButton!
var btn4: UIButton!
var lastSender: UIButton!
var categoryViewController: CategoryViewController?
var subCategoryViewController: SubCategoryViewController?
var scoreViewController: ScoreViewController?
var profileViewController: ProfileViewController?
override func viewDidLoad() {
super.viewDidLoad()
self.setup()
tabBarView = Bundle.main.loadNibNamed("MyTabBar", owner: nil, options: nil)?.last as! UIView
tabBarView.frame = CGRect(x: 0.0, y: self.view.frame.size.height - tabBarView.frame.size.height, width: tabBarView.frame.size.width, height: tabBarView.frame.size.height)
self.view.addSubview(tabBarView)
btn1 = tabBarView.viewWithTag(1) as? UIButton
btn1.addTarget(self, action: #selector(self.processBtn), for: .touchUpInside)
btn2 = tabBarView.viewWithTag(2) as? UIButton
btn2.addTarget(self, action: #selector(self.processBtn), for: .touchUpInside)
btn3 = tabBarView.viewWithTag(3) as? UIButton
btn3.addTarget(self, action: #selector(self.processBtn), for: .touchUpInside)
btn4 = tabBarView.viewWithTag(4) as? UIButton
btn4.addTarget(self, action: #selector(self.processBtn), for: .touchUpInside)
let width1 = self.view.frame.width/4
btn1.frame = CGRect(x: 0, y: 0, width: width1, height: tabBarView.frame.size.height)
btn2.frame = CGRect(x: btn1.frame.width, y: 0, width: width1, height: tabBarView.frame.size.height)
btn3.frame = CGRect(x: btn2.frame.origin.x+btn2.frame.width, y: 0, width: width1, height: tabBarView.frame.size.height)
btn4.frame = CGRect(x: btn3.frame.origin.x+btn3.frame.width, y: 0, width: width1, height: tabBarView.frame.size.height)
lastSender = btn1
selectedViewController = viewControllers?[0]
}
func processBtn(_ sender: UIButton) {
lastSender = sender
selectedViewController = viewControllers?[sender.tag - 1]
if sender.tag == 1 {
btn1.backgroundColor = UIColor.red
btn2.backgroundColor = UIColor.yellow
btn3.backgroundColor = UIColor.yellow
btn4.backgroundColor = UIColor.yellow
}else if sender.tag == 2 {
btn1.backgroundColor = UIColor.yellow
btn2.backgroundColor = UIColor.red
btn3.backgroundColor = UIColor.yellow
btn4.backgroundColor = UIColor.yellow
}else if sender.tag == 3 {
btn1.backgroundColor = UIColor.yellow
btn2.backgroundColor = UIColor.yellow
btn3.backgroundColor = UIColor.red
btn4.backgroundColor = UIColor.yellow
}else if sender.tag == 4 {
btn1.backgroundColor = UIColor.yellow
btn2.backgroundColor = UIColor.yellow
btn3.backgroundColor = UIColor.yellow
btn4.backgroundColor = UIColor.red
}
}
func setup() {
var viewControllers = [AnyObject]()
categoryViewController = self.storyboard!.instantiateViewController(withIdentifier: "CategoryViewController") as? CategoryViewController
viewControllers.append(categoryViewController!)
subCategoryViewController = self.storyboard!.instantiateViewController(withIdentifier: "SubCategoryViewController") as? SubCategoryViewController
viewControllers.append(subCategoryViewController!)
scoreViewController = self.storyboard!.instantiateViewController(withIdentifier: "ScoreViewController") as? ScoreViewController
viewControllers.append(scoreViewController!)
profileViewController = self.storyboard!.instantiateViewController(withIdentifier: "ProfileViewController") as? ProfileViewController
viewControllers.append(profileViewController!)
self.viewControllers = viewControllers as? [UIViewController]
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
for view in tabBarView.subviews as [UIView] {
if let btn = view as? UIButton {
if btn == lastSender {
btn.isSelected = true
}
else {
btn.isSelected = false
}
}
}
if self.selectedViewController == viewController {
(self.selectedViewController as? UINavigationController)?.popToRootViewController(animated: true)
// pop to root if tapped the same controller twice
}
return (viewController != tabBarController.selectedViewController)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
This way you can design custom tabbar as per your need.
Thanks