Underline Tabbar ios swift
In this story we will continue with previous Single View Application Xcode project.
You can read the above story for basic understanding of UITabbar.
What we see today.
- Underline TabBar
- TabBar shadow color
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
1. Underline TabBar
Create a UITabBarController class
Add the class to tabbarviewcontroller
By using the extension of UIImage
extension UIImage {func createSelectionIndicator(color: UIColor, size: CGSize, lineWidth: CGFloat) -> UIImage {UIGraphicsBeginImageContextWithOptions(size, false, 0)color.setFill()UIRectFill(CGRect(x: 0, y: size.height — lineWidth, width: size.width, height: lineWidth))let image = UIGraphicsGetImageFromCurrentImageContext()UIGraphicsEndImageContext()return image!}}
call function in viewdidload
tabBar.selectionIndicatorImage = UIImage().createSelectionIndicator(color: .green, size: CGSize(width: tabBar.frame.width/CGFloat(tabBar.items!.count), height: tabBar.frame.height), lineWidth: 3.0)
It will look something like below code
Now run your app you will get the below beautiful screen.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
2. TabBar shadow color
Continue the previous topic just add below code in viewdidload.
tabBar.layer.shadowColor = UIColor.lightGray.cgColortabBar.layer.shadowOpacity = 0.5tabBar.layer.shadowOffset = CGSize.zerotabBar.layer.shadowRadius = 5self.tabBar.layer.borderColor = UIColor.clear.cgColorself.tabBar.layer.borderWidth = 0self.tabBar.clipsToBounds = falseself.tabBar.backgroundColor = UIColor.whiteUITabBar.appearance().shadowImage = UIImage()UITabBar.appearance().backgroundImage = UIImage()
Now run your app you will again get the below beautiful screen.
Complete project link:
Thank you!!! Happy coding.