zoukankan      html  css  js  c++  java
  • 修改TabBar的背景颜色

    最近在项目种遇到设置tabbar背景色的需求,从网上查找资料,大概有两种方法,一种是View初始化的时候插入一个背景色的UIView,另外一种重写一个tabbar的函数,可是对于这个通过重写tabbar实现设置背景色的函数,由于个人能力的原因,到现在也没有实现,如果有那个网友能够实现这个功能,麻烦请留言给我,告诉我,我也好学习学习。对于第一通过添加背景色View的方式,也是一种可行的方法,可是存在bug,当iphone处于纵向模式,正常显示,可是一旦使手机处于横向模式就出现了问题:对比下图所示:

    通过上图的对比会发现,纵向的时候背景色宽度没有和屏幕宽度一致

    在之此方法的基础上修改了代码,基本上解决了上面的问题。参考代码如下所示:

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

    {

      if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))//根据不同的方向传入不同的设置背景色参数

      {

        [self SetTabbarBgColor:1];

      }

      else 

      {

        [self SetTabbarBgColor:2];

      }

      return YES;

    }

     

    -(void)SetTabbarBgColor:(int)oration //这个函数,要在头文件中做声明,否则编译弹出警告

    {

      CGRect frame =CGRectMake(0, 0, 0, 0);

      if (oration==1)

      {

        frame = CGRectMake(0,0,320,48);//tabbar栏的高度在横向、纵向模式下均是48

      }

      else if(oration ==2)

      {

        frame = CGRectMake(0,0, 480, 48); //tabbar栏的高度在横向、纵向模式下均是48

      }

      UIView *bgView = [[UIView alloc] initWithFrame:frame];//创建一个背景色的View对象

      [bgView setTag:100];//set the bgView tag ,it's very important for this function 

      UIColor *bgcolor = [UIColor blueColor];//Create the color for the bgView object 

      [bgView setBackgroundColor:bgcolor];

      //

      NSArray *TabBarSubView = [[self tabBar] subviews];

      for(UIView *CurrentView in TabBarSubView) 

      {

        NSInteger tabBarItemTag = [CurrentView tag];

        if (tabBarItemTag==100)

        {

          [CurrentView removeFromSuperview];//remove the old bgColorView

          break;

        }

      }

      //

      [self.tabBar insertSubview:bgView atIndex:0];

        [bgView release];//release the resources ,save the system memorey

    }

     

    修改后,运行程序,效果如下所示:

     

    如果那位网友有更好的方法,请告诉我。

     

    THE END !

    2011-07-09

  • 相关阅读:
    eclipse——插件报错:Could not find node.js
    常用css设置
    前端跨域常见的处理方法
    刷新建设批次为空的sql
    PostgreSQL 添加字段语句
    查询在A表有记录数据,B表没有记录数据的SQL
    postgreSql 备份复制表结构和数据 SQL语句
    删除site_planning_id 和version 重复的,如果有多个版本,留下版本号最高的
    leetcode 精选top面试题
    leetcode 精选top面试题
  • 原文地址:https://www.cnblogs.com/xingchen/p/2101704.html
Copyright © 2011-2022 走看看