ios更改UITabBarController背景以及选中背景图片的方法

 2023-09-10 阅读 13 评论 0

摘要:不多说,直接上方案。一、背景图片1、5.0以上版本UIImage *image = [UIImage imageNamed:@"system_tabbar_bg.png"];[self.tabBar setBackgroundImage:image];2、5.0以下版本UIImage *image = [UIImage imageNamed:@"system_tabbar_bg.png&

 不多说,直接上方案。
  一、背景图片
  1、5.0以上版本
     UIImage *image = [UIImage imageNamed:@"system_tabbar_bg.png"];
     [self.tabBar setBackgroundImage:image];
  2、5.0以下版本
     UIImage *image = [UIImage imageNamed:@"system_tabbar_bg.png"];
     NSArray *array = [self.view subviews];
     UITabBar *tabBar = [array objectAtIndex:1];
     tabBar.layer.contents = (id)image.CGImage;
 
  二、选中的item的背景图片设置
  1、5.0以上版本
     self.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"system_tabbar_item_selected.png"];
  2、5.0以下版本
     首先实现如下方法:
- (void)setNoHighlistTabBar:(UITabBarController *)tabBarController
{
    NSArray * tabBarSubviews = [tabBarController.tabBar subviews];
   
    int index4SelView;
   
    if(tabBarController.selectedIndex+1 > 4)
    {//selected the last tab.
        index4SelView = [tabBarSubviews count]-1;
    }
    else if([tabBarController.viewControllers count] > 5)
    {//have "more" tab. and havn't selected the last tab:"more" tab.
       
       
        index4SelView = [tabBarSubviews count] - 5 + tabBarController.selectedIndex;
    }
    else
    {//have no "more" tab.
       
       
        index4SelView = [tabBarSubviews count] -
        [tabBarController.viewControllers count] + tabBarController.selectedIndex;
    }
    if([tabBarSubviews count] < index4SelView+1)
    {
        assert(false);
        return;
    }
    UIView * selView = [tabBarSubviews objectAtIndex:index4SelView];
   
    NSArray * selViewSubviews = [selView subviews];
   
    for(UIView * v in selViewSubviews)
    {
        if(v && [NSStringFromClass([v class]) isEqualToString:@"UITabBarSelectionIndicatorView"])
       
        {//the v is the highlight view.
            [self.selectedItemBgImageView removeFromSuperview];
            [selView insertSubview:self.selectedItemBgImageView belowSubview:v];
           
            [v removeFromSuperview];
           
           
            break;

        }
    }
}
  改方法的实质就是循环tabBar的subViews, 找到tabBar中的这个view, 是一个UITabBarSelectionIndicatorView的view,然后把它替换成你自己创建的UIImageView, 上例中的self.selectedItemBgImageView.
  然后需要把UITabBarController的delegate设为self, 在tabBarController:didSelectViewController的代理方法中执行上面的方法:[self setNoHighlistTabBar:self];
 还有setSelectIndex:方法中也要执行[self setNoHighlistTabBar:self];

转载于:https://www.cnblogs.com/ubersexual/p/3259795.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/38557.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息