I want to add a selection indicator to a UITabbarItem in Swift, like the following image.
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?