-1

I want to add a selection indicator to a UITabbarItem in Swift, like the following image.

enter image description here

I tried to code below but I cannot add the circle above the UITabBar.

On viewDidLoad:

 let tabBar = self.tabBarController!.tabBar
    tabBar.selectionIndicatorImage = UIImage().createSelectionIndicator(color: UIColor.red, size: CGSizeMake(tabBar.frame.width/CGFloat(tabBar.items!.count), tabBar.frame.height), lineWidth: 8.0)
extension UIImage {
    func createSelectionIndicator(color: UIColor, size: CGSize, lineWidth: CGFloat) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        let innerRect = CGRect(x: (size.width/2) - lineWidth/2,
                                    y: -2,
                                    width: lineWidth,
                                    height: lineWidth)

        let path = UIBezierPath(roundedRect: innerRect, cornerRadius: lineWidth/2)
        path.fill()

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
     }
}

Can you help me?

Louiza A
  • 23
  • 6

1 Answers1

0

I will suggest you to use different image with circle for selected state. If you use SVG image with custom size, it will be better. You can change it from storyboard.

check the storyboard screenshot.

Supriyo Dey
  • 230
  • 1
  • 12