0

Here are two colors I have:

static let mainDarkBlue = UIColor(r: 40, g: 51, b: 86)
static let mainDarkBlueTransparent = UIColor(r: 40, g: 51, b: 86, a: 0.8)

I have mainDarkBlue set for a barTint color and that's fine, but there is mainDarkBlueTransparent which I want to be a selected tabBar item's background color.

tabBar.barTintColor = UIColor.mainDarkBlue

That works fine, but I want to make the color-difference between selected and unselected of my two items.
Also btw, if anyone knows how to make the selected item's shadow please show me.

stackich
  • 3,607
  • 3
  • 17
  • 41
  • 1
    I would suggest you, to make `static let mainDarkBlueTransparent = mainDarkBlue .withAlphaComponent(0.8)` and when you change `mainRadBlue` color, you won't need to search for this rgb numbers and change somewhere else. – vpoltave Sep 09 '20 at 11:43
  • Yeah you're right. – stackich Sep 09 '20 at 11:45
  • Does this answer your question? [Set background color of active tab bar item in Swift](https://stackoverflow.com/questions/29045147/set-background-color-of-active-tab-bar-item-in-swift) – vpoltave Sep 09 '20 at 11:50
  • No, it makes no change. When I put tabbar.isTranslucent = true, it makes good changes, but the color is not clear if you know what I mean, it is ugly and making something like little animation when I am selecting tabbar items. – stackich Sep 09 '20 at 12:08

1 Answers1

0

To set a background color for the selected tabBar-item you could do the following:

extension UIColor {
    func image(_ size: CGSize = CGSize(width: 71, height: 48)) -> UIImage {
        return UIGraphicsImageRenderer(size: size).image { rendererContext in
            self.setFill()
            rendererContext.fill(CGRect(origin: .zero, size: size))
        }
    }
}

This extension can be used to turn a color into an image. This image can then be used as the selectionIndicatorImage.

tabBar.selectionIndicatorImage = UIColor.mainDarkBlueTransparent.image()

To be more flexible, you could also do the following (based on the link provided by vpoltave):

let numberOfItems = CGFloat(tabBar.items!.count)//make sure that items isn't nil -> maybe you should use some if let or guard instead + make sure that your are executing this code after you have set the tabBarItems
let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
tabBar.selectionIndicatorImage = UIColor.mainDarkBlueTransparent.image(tabBarItemSize)
finebel
  • 2,227
  • 1
  • 9
  • 20
  • It just changes a color of the small image I have in tabbar item, not the backgroud color ... – stackich Sep 09 '20 at 11:46
  • I cannot use 71 and 48 for height and width, I need my tabbar's sizes but cannot catch them in extension. Also, where did you define that setFill() function? – stackich Sep 09 '20 at 12:05
  • ```setFill()``` is defined inside ```UIColor``` (https://developer.apple.com/documentation/uikit/uicolor/1621926-setfill). So you can just copy the entire extension somewhere into your porject... I have edited my question again to deal with other tabBarItemSize(s). – finebel Sep 09 '20 at 12:08