I am using UITabBar, not UITabBarController. Whenever user clicks on any tab, I am able to see the underlined image.
public override func viewDidLoad() {
super.viewDidLoad()
UITabBar.appearance().selectionIndicatorImage = getImageWithColorPosition(color: UIColor.darkGray, size: CGSize(width:presenterView.frame.size.width/2, height:49), lineSize: CGSize(width:presenterView.frame.size.width/2, height:2))
}
//getImageWithColorPosition is for adding underlined image below UITabBarItem
func getImageWithColorPosition(color: UIColor, size: CGSize, lineSize: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let rectLine = CGRect(x: 0, y: size.height-lineSize.height, width: lineSize.width, height: lineSize.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
UIColor.clear.setFill()
UIRectFill(rect)
color.setFill()
UIRectFill(rectLine)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
openItem1()
}
public func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if (item.tag == 0) {
openItem1()
} else {
openItem2()
}
}
However, when user clicks on any of the tab items then only underlined image is displayed. I want to select a default tab when user lands on the screen for the first time. As I am doing it programmatically, the underline image doesn't show up. I read couple of stack overflow posts which say that selection on UITabBar can not be triggered programmatically.
Is there any other way to go about it?