iOS 自定义UITabBarController的tabBar

时间:2024-01-13 18:56:14

iOS 自定义UITabBarController的tabBar   iOS 自定义UITabBarController的tabBar   iOS 自定义UITabBarController的tabBar    iOS 自定义UITabBarController的tabBar

#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 = ;
//设置代理
_tabBarController.delegate = self;
_tabBarController.viewControllers = array;
imgWidth = [UIScreen mainScreen].bounds.size.width/(array.count*1.0);
//初始化tabBarController
[self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"11.png" selectedImageViewTag: selectedImage:@"111.png" tabBarItemTitle:@"首页"];
[self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"12.png" selectedImageViewTag: selectedImage:@"112.png" tabBarItemTitle:@"导航"];
[self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"13.png" selectedImageViewTag: selectedImage:@"113.png" tabBarItemTitle:@"消息"];
[self setupView:CGRectMake(imgWidth*, , imgWidth, ) image:@"14.png" selectedImageViewTag: selectedImage:@"114.png" tabBarItemTitle:@"更多"];
//设置文字的颜色
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:UITextAttributeTextColor] forState:];
//设置选中时文字的颜色
[[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-) == ) {
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 - )];
//设置标题
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 == +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