tabBar用block實現自訂

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   檔案   for   ar   

- (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);}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.