zoukankan      html  css  js  c++  java
  • 自定义的TabBar

    //
    //  AppDelegate.m
    //  CustomTabBar-0818
    //
    //  Created by apple on 14-8-18.
    //  Copyright (c) 2014年 apple. All rights reserved.
    //
    
    #import "AppDelegate.h"
    #import "Items.h"
    #import "TabBar.h"
    @interface AppDelegate ()<TabBarDelegate>
    {
        UITabBarController *tabBarViewContr;
    }
    
    @end
    
    @implementation AppDelegate
                
    
    - (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;
        
        
        [tabBarViewContr.tabBar addSubview:tabBar1];
        
        
        self.window.rootViewController=tabBarViewContr;
        return YES;
    }
    
    - (void)tabBar:(TabBar *)tabBar didTag:(NSInteger)tag
    {
        UITabBarController *tabBarViewCon=(UITabBarController *)self.window.rootViewController;
        tabBarViewCon.selectedIndex=tag;
    //    tabBarViewContr.selectedIndex=tag;
    }
    
    - (void)applicationWillResignActive:(UIApplication *)application {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    @end
    #import <Foundation/Foundation.h>
    #import "UIKit/UIKit.h"
    @class TabBar;
    
    @protocol TabBarDelegate <NSObject>
    
    @optional
    - (void)tabBar:(TabBar *)tabBar didTag:(NSInteger)tag;
    
    @end
    @interface TabBar : UIView
    @property(nonatomic,strong)NSArray *itemArray;
    @property(nonatomic,strong)id<TabBarDelegate> delegate;
    @end
    
    
    
    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];
        }
    }
  • 相关阅读:
    韩信的糊涂
    用友U8两个怪问题
    写给对前途迷茫的朋友:五句话定会改变你的人生
    换博客了
    各种杀毒工具的优缺点
    又是一年中秋到 别有一般更思乡
    谁说黑夜是孤单的
    李想:创业不一定是创办企业
    SQ小组KTV点歌系统简介
    注意!在subList生成子列表之后,一定不要随便更改原列表
  • 原文地址:https://www.cnblogs.com/lidongq/p/3920624.html
Copyright © 2011-2022 走看看