iOS:如何在UITabBarItem中添加下划线 [英] iOS : How to add Underline in UITabBarItem

查看:205
本文介绍了iOS:如何在UITabBarItem中添加下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在申请工作,我需要在 UITabbarItem 中添加下划线。

I am working in a application where i need to add underline in UITabbarItem.

所以我想要在iOS的默认 UITabbarcontroller 中的所选 UITabbarItem 下添加下划线。

so i would like to add underline under the selected UITabbarItem in Default UITabbarcontroller of iOS.

我已经创建了 UITabbarcontroller 的子类,但没有找到任何方法来添加行。

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.

推荐答案

请尝试这个。
我曾在我的应用程序中使用过一次,并希望它也能帮到你。

Please try this one. I have used in my application once and hope it will help you too.

这就是我以编程方式创建Tab栏的方式:

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;
}

这篇关于iOS:如何在UITabBarItem中添加下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆