zoukankan      html  css  js  c++  java
  • iOS-UITabbar自定义

    [self createCustomTabBar];

    -(void)createCustomTabBar{
        //创建一个UIImageView,作为底图
        UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-49, 320, 49)];
        bgView.image = [UIImage imageNamed:@"tabbg.png"];
        bgView.tag = 999;
        //开启imageview的交互属性
        bgView.userInteractionEnabled = YES;
        [self.view addSubview:bgView];
       
        
        //创建button实例  模拟tabBarItem
        //每个button的间
        NSArray *array=[NSArray arrayWithObjects:@"IconSettings",@"IconProfile",@"IconHome",@"IconEmpty",@"IconCalendar",@"btn_bottom1_on.png",@"btn_bottom2_on.png",@"btn_bottom3_on.png",@"btn_bottom4_on.png",@"btn_bottom5_on.png", nil];
        for (int i = 0; i<(array.count)/2; i++) {
            NSString *imageName = [array objectAtIndex:i];
            NSString *selectedImage = [array objectAtIndex:i+5];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setFrame:CGRectMake(0+64*i, 0, 64, 49)];
            //设置button处于常规状态下的背景图片
            [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
            //设置button处于选中状态下得背景图片
            [btn setBackgroundImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
            [btn addTarget:self
                    action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = i;
            
            if (i==0) {
                btn.selected = YES;
            }
            
            [bgView addSubview:btn];
        }
    }
    -(void)btnClicked:(UIButton *)btn
    {
        //点击不同的按钮,切换不同的视图控制器
        self.selectedIndex = btn.tag;
        //切换不同btn的显示状态
        UIImageView *bgView = (UIImageView *)[self.view viewWithTag:999];
        for (UIView *subView in bgView.subviews) {
            if ([subView isKindOfClass:[UIButton class]]) {
                UIButton *btn1 = (UIButton *)subView;
                if (btn1.tag == btn.tag) {
                    btn1.selected = YES;
                }else{
                    btn1.selected = NO;
                }
            }
        }
    }

  • 相关阅读:
    第六章 虹销雨霁(中)
    第四章 曙光初现(下)
    第三章 曙光初现(上)
    第二章 福祸相伴(下)
    第二章 福祸相伴(上)
    小云(云层-陈霁)的发展史
    小白成长建议(9)-苞丁解牛
    NYoj 116士兵杀敌(二)区间求和,单点更新
    HDU 1754 区间查询,单点更新
    《灯亮or灯灭》 --有个有趣的数论问题
  • 原文地址:https://www.cnblogs.com/linxiu-0925/p/5422113.html
Copyright © 2011-2022 走看看