Search code examples
iosswiftinstagramuitabbaritem

Edit background color of specific Tab bar item


Hey i would like to change the background Color of my center tab bar item like instagram in swift, anyone who knows how it works. I googled but can't find any helpful answer.


Solution

  • You can use the following code :

    // Add background color to middle tabBarItem
    let itemIndex = 2
    let bgColor = UIColor(red: 0.08, green: 0.726, blue: 0.702, alpha: 1.0)
    
    let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
    let bgView = UIView(frame: CGRectMake(itemWidth * itemIndex, 0, itemWidth, tabBar.frame.height))
    bgView.backgroundColor = bgColor
    tabBar.insertSubview(bgView, atIndex: 0)
    

    For reference you can look into these Stackoverflow posts :