0

I have 5 tab bar items and in the middle, I have a large circular button and I want to disable the label (Workout) of this large button but the color of the label should not be affected. I have disabled the workout tab bar selection by defining the tag value to the workout item bar. But It has also disabled the button(+) but I want to disable the workout label only. How would I achieve this? Anybody has any idea, please help me out

tab bar controller code:

import UIKit
import SwiftIcons

class HomeViewController: UITabBarController, UITabBarControllerDelegate {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        
        let selectedColor   = UIColor(red: 16.0/255.0, green: 78.0/255.0, blue: 91.0/255.0, alpha: 1.0)
        let unselectedColor = UIColor(red: 16.0/255.0, green: 224.0/255.0, blue: 223.0/255.0, alpha: 1.0)

        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: unselectedColor], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: selectedColor], for: .selected)
        
            
        //store every image in a variable
        
        let userUnselectedImage: UIImage = UIImage(named: "add_workout_icon")!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        let userSelectedImage: UIImage = UIImage(named: "add_workout_icon")!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        
            tabBar.items![2].image = userUnselectedImage
            tabBar.items![2].selectedImage = userSelectedImage
        
        
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        let tagValue = viewController.tabBarItem.tag
        if tagValue == 2 {
            return false
        }
        
        return true
    }
    
}

custom tab bar class

import UIKit
@IBDesignable
class TabBarShape: UITabBar {
    private var shapeLayer: CALayer?
    private func addShape() {
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = createPath()
        shapeLayer.strokeColor = UIColor.lightGray.cgColor
        shapeLayer.fillColor = UIColor.white.cgColor
        shapeLayer.lineWidth = 1.0
        
        //The below 4 lines are for shadow above the bar. you can skip them if you do not want a shadow
        shapeLayer.shadowOffset = CGSize(width:0, height:0)
        shapeLayer.shadowRadius = 10
        shapeLayer.shadowColor = UIColor.gray.cgColor
        shapeLayer.shadowOpacity = 0.3

        if let oldShapeLayer = self.shapeLayer {
            self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
        } else {
            self.layer.insertSublayer(shapeLayer, at: 0)
        }
        self.shapeLayer = shapeLayer
    }
    override func draw(_ rect: CGRect) {
        self.addShape()
    }
    func createPath() -> CGPath {
        let height: CGFloat = 35.0
        let path = UIBezierPath()
        let centerWidth = self.frame.width / 2
        path.move(to: CGPoint(x: 0, y: 0)) // start top left
        path.addLine(to: CGPoint(x: (centerWidth - height * 2) + 30, y: 0)) // the beginning of the trough
        //path.addLine(to: CGPoint(x: 150, y: 0)) // hardcoded
        print("x=>\((centerWidth - height * 2) + 37.5)")

        path.addCurve(to: CGPoint(x: centerWidth, y: height),
        controlPoint1: CGPoint(x: (centerWidth - 30), y: 0), controlPoint2: CGPoint(x: centerWidth - 35, y: height))

        path.addCurve(to: CGPoint(x: (centerWidth + height * 2) - 30 , y: 0),
        //path.addCurve(to: CGPoint(x: 230, y: 0),
        controlPoint1: CGPoint(x: centerWidth + 35, y: height), controlPoint2: CGPoint(x: (centerWidth + 30), y: 0))
        
        print("x2=>\((centerWidth + height * 2))")

        path.addLine(to: CGPoint(x: self.frame.width, y: 0))
        path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
        path.addLine(to: CGPoint(x: 0, y: self.frame.height))
        path.close()
        

        return path.cgPath
    }
    
}

UI image enter image description here

1 Answers1

0

remove this(Because don't need it do that.) func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool do it like below

override func viewDidLoad() {
    super.viewDidLoad()
    
    self.delegate = self
}

// UITabBarDelegate
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    print("Selected item")
    let items = self.tabBar.items
    for (i,it) in items!.enumerated() {
        if i == 2 && it == item{
            self.tabBar.tintColor = .gray
        }else {
            self.tabBar.tintColor = .red
        }
    }
}

make sure you changes in UITabBarController class.