I am working in a application where i need to add underline in UITabbarItem
.
so i would like to add underline under the selected UITabbarItem
in Default UITabbarcontroller
of iOS.
I have already created subclass of UITabbarcontroller
but didn't find any way to add line in that.
I want to do something like below image.
If anyone have any idea for this please share here.
Code for Swift 3
In didFininshLaunch call method:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UITabBar.appearance().selectionIndicatorImage = getImageWithColorPosition(color: UIColor.blue, size: CGSize(width:(self.window?.frame.size.width)!/4,height: 49), lineSize: CGSize(width:(self.window?.frame.size.width)!/4, height:2))
return true
}
Method:
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: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
Please try this one. I have used in my application once and hope it will help you too.
This is how I have created Tab bar programmatically:
UITabBarController *tab = [[UITabBarController alloc]init];
ViewController1 *v1 = [[ViewController1 alloc]init];
ViewController1 *v2 = [[ViewController1 alloc]init];
ViewController1 *v3 = [[ViewController1 alloc]init];
ViewController1 *v4 = [[ViewController1 alloc]init];
tab.viewControllers = [NSArray
arrayWithObjects:v1,v2,v3,v4,nil];
[[tab.tabBar.items objectAtIndex:0]setImage:[UIImage imageNamed:@""]];
[[tab.tabBar.items objectAtIndex:1]setImage:[UIImage imageNamed:@""]];
[[tab.tabBar.items objectAtIndex:2]setImage:[UIImage imageNamed:@""]];
[[tab.tabBar.items objectAtIndex:3]setImage:[UIImage imageNamed:@""]];
int divide = 4;
if([UIDevice currentDevice].userInterfaceIdiom ==UIUserInterfaceIdiomPad)
divide =6;
}
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(tab.tabBar.frame.origin.x,tab.tabBar.frame.origin.y, self.view.frame.size.width/divide, 56)];
UIImageView *border = [[UIImageView alloc]initWithFrame:CGRectMake(view.frame.origin.x,view.frame.size.height-6, self.view.frame.size.width/divide, 6)];
border.backgroundColor = “your color”;
[view addSubview:border];
[tab.tabBar setSelectionIndicatorImage:[self changeViewToImage:view]];
//This is the method that will draw the underline
-(UIImage ) changeViewToImage : (UIView ) viewForImage {
UIGraphicsBeginImageContext(viewForImage.bounds.size);
[viewForImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With