#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
/** * 素材图片的链接: http://pan.baidu.com/s/1geahYRT 密码: axmh * 注意图片的尺寸,否则会变形 */ #import "AppDelegate.h" #import "FirstViewController.h" #import "SecondViewController.h" #import "ThirdViewController.h" #import "FourthViewController.h" @interface AppDelegate ()<UITabBarControllerDelegate> { float imgWidth;//tabBarController的宽度 UITabBarController *_tabBarController; NSMutableArray *imageViewArr; } @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]; //初始化数组 imageViewArr = [[NSMutableArray alloc] init]; //初始化控制器 FirstViewController *firstVC = [[FirstViewController alloc] init]; SecondViewController *secondVC = [[SecondViewController alloc] init]; ThirdViewController *thirdVC = [[ThirdViewController alloc] init]; FourthViewController *fourthVC = [[FourthViewController alloc] init]; NSArray *array = @[firstVC,secondVC,thirdVC,fourthVC]; //初始化UITabBarController _tabBarController = [[UITabBarController alloc] init]; //背景图片 [_tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"1.png"]]; //默认选中第一个 _tabBarController.selectedIndex = 0; //设置代理 _tabBarController.delegate = self; _tabBarController.viewControllers = array; imgWidth = [UIScreen mainScreen].bounds.size.width/(array.count*1.0); //初始化tabBarController [self setupView:CGRectMake(imgWidth*0, 0, imgWidth, 49) image:@"11.png" selectedImageViewTag: 1000 selectedImage:@"111.png" tabBarItemTitle:@"首页"]; [self setupView:CGRectMake(imgWidth*1, 0, imgWidth, 49) image:@"12.png" selectedImageViewTag:1001 selectedImage:@"112.png" tabBarItemTitle:@"导航"]; [self setupView:CGRectMake(imgWidth*2, 0, imgWidth, 49) image:@"13.png" selectedImageViewTag:1002 selectedImage:@"113.png" tabBarItemTitle:@"消息"]; [self setupView:CGRectMake(imgWidth*3, 0, imgWidth, 49) image:@"14.png" selectedImageViewTag:1003 selectedImage:@"114.png" tabBarItemTitle:@"更多"]; //设置文字的颜色 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:UITextAttributeTextColor] forState:0]; //设置选中时文字的颜色 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor] forState:UIControlStateSelected]; self.window.rootViewController = _tabBarController; [self.window makeKeyAndVisible]; return YES; } /** * 初始化tabBarController的tabBar * * @param frame 图片的尺寸 * @param imageName 底层图片的名字 * @param tag 图片的标签 * @param selectedImageName 顶层图片的名字 * @param title 标题 */ - (void)setupView:(CGRect)frame image:(NSString *)imageName selectedImageViewTag:(int)tag selectedImage:(NSString *)selectedImageName tabBarItemTitle:(NSString*)title{ //底层图片 UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame]; imgView.image = [UIImage imageNamed:imageName]; //顶层图片(不选中时隐藏) UIImageView *selectedImageView = [[UIImageView alloc] initWithFrame:frame]; selectedImageView.tag = tag; selectedImageView.image = [UIImage imageNamed:selectedImageName]; if ((tag-1000) == 0) { selectedImageView.hidden = NO; }else{ selectedImageView.hidden = YES; } [_tabBarController.tabBar addSubview:imgView]; [_tabBarController.tabBar addSubview:selectedImageView]; UITabBar *tabBar = _tabBarController.tabBar; UITabBarItem *tabBarItem = [tabBar.items objectAtIndex:(tag - 1000)]; //设置标题 tabBarItem.title = title; //selectedImageView存放到数组 [imageViewArr addObject:selectedImageView]; } #pragma mark -- UITabBarControllerDelegate的方法 -- - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ //遍历imageView,选中的就不隐藏,其它的隐藏 for (UIImageView *imageView in imageViewArr) { if (imageView.tag == 1000+tabBarController.selectedIndex) { imageView.hidden = NO; }else{ imageView.hidden = YES; } } } @end
#import <UIKit/UIKit.h> @interface FirstViewController : UIViewController @end
#import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @end
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor orangeColor]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface ThirdViewController : UIViewController @end
#import "ThirdViewController.h" @interface ThirdViewController () @end @implementation ThirdViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface FourthViewController : UIViewController @end
#import "FourthViewController.h"
@interface FourthViewController ()
@end
@implementation FourthViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@end