Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS : How to add Underline in UITabBarItem

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.

UITabbarWithUnderline Image

If anyone have any idea for this please share here.

like image 378
Malav Soni Avatar asked Sep 19 '15 13:09

Malav Soni


2 Answers

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
    }
like image 54
ajay_nasa Avatar answered Oct 18 '22 23:10

ajay_nasa


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;
}
like image 39
AP7 Avatar answered Oct 18 '22 23:10

AP7