zoukankan      html  css  js  c++  java
  • ios更改UITabBarController背景以及选中背景图片的方法

    一、背景图片
      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];

    ============以下为20200220 更新===========

    在13 的新系统上 bar的文字选中色在经过push操作后变蓝,如上图所示.这个是ios 13 系统的一个bug。需要程序员来填坑。

    解决办法:在设置tabbar相关appearance的代码处,再单独加一个13系统的处理即可:

    if(@available(iOS13.0, *)) {

            [[UITabBar appearance] setTintColor:[UIColor greenColor]];

            [[UITabBar appearance] setUnselectedItemTintColor:[UIColor grayColor]];

        }



    更多链接请看
    链接:
    https://www.jianshu.com/p/96096df52b53
    https://www.jianshu.com/p/82b94ceb38d0
    https://www.jianshu.com/p/a5461adff3ae
  • 相关阅读:
    【百度之星2014~初赛(第二轮)解题报告】Chess
    Cocos2d-x3.0游戏实例之《别救我》第二篇——创建物理世界
    【CSS】使用CSS控制文字过多自动省略号
    【jar】JDK将单个的java文件打包为jar包,并引用到项目中使用【MD5加密】
    【JSP EL】el表达式判断是否为null
    【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation
    【Exception】查看异常出现在具体的文件名/类名/方法名/具体行号
    【bootstrap】使用支持bootstrap的时间插件daterangepicker
    【css】设置div位于浏览器的最底层,离用户最远
    【前台】【单页跳转】整个项目实现单页面跳转,抛弃iframe
  • 原文地址:https://www.cnblogs.com/isItOk/p/4875569.html
Copyright © 2011-2022 走看看