iOS UITabBarController tabbar 自定义 详解


UITabBarController的item间距能不能设置 

UITabBarController的每个item不设title的话,那个title还是会占据着最下面那一行的位置,该怎么去隐藏它让那个位置空出来

UITabBarController的item间距能不能设置,因为我发现只有3个item的时候都集中在中间那块,我想把它们的间距设置大一点

可以换一种方案,调整UIBarItem的imageInsets使图片居中 (我假设你真正想要的是让图片居中显示)

<code style="font-family: 'Source Code Pro', Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; color: inherit; padding: 0px; white-space: inherit; background: none;"><span class="hljs-title" style="color: rgb(38, 139, 210);">[self.tabBarItem setImageInsets:UIEdgeInsetsMake(10, 0, -10, 0)]</span><span class="hljs-comment" style="color: rgb(147, 161, 161);">;</span>
</code>

很重要的一点,设置UIEdgeInsetsMaketopbottom属性为相反数,
否则会发生点击item后拖动,item会发生形变。

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  RootViewController.m  
  3. //  test_storyBoard_ipad  
  4. //  
  5. //  Created by admin on 3/4/16.  
  6. //  Copyright © 2016 jeffasd. All rights reserved.  
  7. //  
  8.   
  9. #import "RootViewController.h" //父类 UITabbarController  
  10.   
  11. @interface RootViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation RootViewController  
  16.   
  17. - (void)viewDidLoad {  
  18.     [super viewDidLoad];  
  19.   
  20.       
  21. //    [self.tabBarItem setImageInsets:UIEdgeInsetsMake(10, 0, -10, 0)];  
  22.       
  23. //    [self.tabBarItem setImageInsets:UIEdgeInsetsMake(30, 0, -30, 0)];  
  24.       
  25. //    self.tabBar.items  
  26.       
  27.     for (UITabBarItem *item in self.tabBar.items) {  
  28.           
  29. //        UIEdgeInsets insets = {top, left, bottom, right};  
  30.           
  31. //        [item setImageInsets:UIEdgeInsetsMake(10, 0, -10, 0)];  
  32.           
  33. //        [item setImageInsets:UIEdgeInsetsMake(0, -30, 0, 30)];  
  34.           
  35.         [item setImageInsets:UIEdgeInsetsMake(0, -30030)];  
  36.           
  37.     }  
  38.       
  39. //    UITabBarButton;  
  40. //    UIButton;  
  41.       
  42.     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  
  43.     button.bounds = CGRectMake(003080);  
  44.     button.backgroundColor = [UIColor redColor];  
  45.     button.center = CGPointMake(self.tabBar.frame.size.width/2self.tabBar.frame.size.height/2 - 20);  
  46.       
  47. //    [self.tabBar setShadowImage:IMG(@"transparent")];  
  48.       
  49. //    button.center = self.tabBar.center;  
  50.       
  51.     [self.tabBar addSubview:button];  
  52.       
  53.     NSLog(@"self tabBar is %@"self.tabBar.subviews);  
  54.       
  55. //    [self.tabBar.superview addSubview:button];  
  56.       
  57. //    [[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];  
  58.       
  59. //    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];  
  60.       
  61.     self.tabBar.backgroundImage = [[self class] createImageWithColor:[UIColor cyanColor]];  
  62.     self.tabBar.shadowImage = [UIImage new];  
  63.       
  64. }  
  65.   
  66. /** 
  67.  * 将UIColor变换为UIImage 
  68.  * 
  69.  **/  
  70. + (UIImage *)createImageWithColor:(UIColor *)color  
  71. {  
  72.     CGRect rect = CGRectMake(0.0f0.0f1.0f1.0f);  
  73.     UIGraphicsBeginImageContext(rect.size);  
  74.     CGContextRef context = UIGraphicsGetCurrentContext();  
  75.     CGContextSetFillColorWithColor(context, [color CGColor]);  
  76.     CGContextFillRect(context, rect);  
  77.     UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();  
  78.     UIGraphicsEndImageContext();  
  79.       
  80.     return theImage;  
  81. }  
  82.   
  83. - (void)didReceiveMemoryWarning {  
  84.     [super didReceiveMemoryWarning];  
  85.     // Dispose of any resources that can be recreated.  
  86. }  
  87.   
  88. /* 
  89. #pragma mark - Navigation 
  90.  
  91. // In a storyboard-based application, you will often want to do a little preparation before navigation 
  92. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
  93.     // Get the new view controller using [segue destinationViewController]. 
  94.     // Pass the selected object to the new view controller. 
  95. } 
  96. */  
  97.   
  98. @end  




https://segmentfault.com/q/1010000000362020

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值