zoukankan      html  css  js  c++  java
  • tabBar用block实现自定义

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        
        NSMutableArray *viewContrs=[[NSMutableArray alloc]init];
        for (int i=0; i<5; i++)
        {
            UIViewController *viewContr=[[UIViewController alloc]init];
            viewContr.view.backgroundColor=[UIColor colorWithRed:i*0.2 green:i*0.2 blue:0.5 alpha:0.8];
            [viewContrs addObject:viewContr];
        }
        //创建标签控制器
        tabBarViewContr=[[UITabBarController alloc]init];
        tabBarViewContr.viewControllers=viewContrs;
        
        //把图片存入5个对象中再存入数组中
        NSMutableArray *items=[[NSMutableArray alloc]init];
        for (int i=0; i<5; i++)
        {
            Items *item=[[Items alloc]init];
            NSString *str=[NSString stringWithFormat:@"%d.jpg",i+1];
            [item setImage:[UIImage imageNamed:str] title:str];
            [items addObject:item];
        }
        
        //自定义的tabBar
        TabBar *tabBar1=[[TabBar alloc]initWithFrame:tabBarViewContr.tabBar.bounds];
        tabBar1.itemArray=items;
    //    tabBar1.delegate=self;
        tabBar1.block=^(TabBar *sender,NSInteger tag){
    //        [self tabBar:sender didTag:tag];
            tabBarViewContr.selectedIndex=tag;
        };
        
    //    tabBarViewContr.tabBar.hidden=YES;
        [tabBarViewContr.tabBar addSubview:tabBar1];
        
        
        self.window.rootViewController=tabBarViewContr;
        return YES;
    }
    
    


    自定义的TabBar实现文件
    - (void)setItemArray:(NSArray *)items
    {
        CGFloat width=self.frame.size.width/items.count;
        for (int i=0; i<items.count; i++)
        {
            Items *item=[items objectAtIndex:i];
            UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame=CGRectMake(i*width, 0, width, self.frame.size.height-15);
            [btn addTarget:self action:@selector(didClicked:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag=i;
            [btn setImage:item.image forState:UIControlStateNormal];
            [self addSubview:btn];
            
            //显示文字
            UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(i*width, self.frame.size.height-15, width, 15)];
            label.text=item.title;
            label.textAlignment=NSTextAlignmentCenter;
            [self addSubview:label];
        }
    }
    
    
    

    - (void)didClicked:(UIButton *)sender { if (_delegate&&[_delegate respondsToSelector:@selector(tabBar:didTag:)]) { [_delegate tabBar:self didTag:sender.tag]; } _block(self,sender.tag); }
  • 相关阅读:
    Trachtenberg(特拉亨伯格)速算系统
    English Conversations You Can Download for Free (Spoken English MP3/Audio Files)
    rel=nofollow 是什么意思
    移动端 触摸事件 ontouchstart、ontouchmove、ontouchend、ontouchcancel
    <mate name="viewport">移动端设置详解
    jquery简单实现tab选项卡效果
    PHP new StdClass()创建空对象
    PHP可变函数
    jQuery中this与$(this)的区别实例
    jQuery给动态添加的元素绑定事件的方法
  • 原文地址:https://www.cnblogs.com/lidongq/p/3925439.html
Copyright © 2011-2022 走看看