UITabBarController的创建等基本方法

时间:2024-01-09 17:27:38
 #import "AppDelegate.h"

 @interface AppDelegate () <UITabBarControllerDelegate>

 @end

 @implementation AppDelegate

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. // 1.创建window
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible]; // 2.创建UITabBarController对象
UITabBarController *mainTabBar = [[UITabBarController alloc] init]; // 创建控制器对象
UIViewController *firstVC = [[UIViewController alloc] init];
firstVC.view.backgroundColor = [UIColor cyanColor]; // 设置tabBarItem
// 第一种方式:系统样式
firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:]; // 第二种方式:自定义样式
UIViewController *secondVC = [[UIViewController alloc] init];
secondVC.view.backgroundColor = [UIColor orangeColor]; // 创建图片
UIImage *secondImg = [UIImage imageNamed:@"carGary@2x"];
UIImage *selectSecondImg = [UIImage imageNamed:@"carRed@2x"]; // 设置图片保留原有样式
secondImg = [secondImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
selectSecondImg = [selectSecondImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"two" image:secondImg selectedImage:selectSecondImg]; UIViewController *thirdVC = [[UIViewController alloc] init];
thirdVC.view.backgroundColor = [UIColor redColor];
thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"findGray@2x"] tag:]; UIViewController *fourthVC = [[UIViewController alloc] init];
fourthVC.view.backgroundColor = [UIColor greenColor]; fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"用户" image:[UIImage imageNamed:@"userGray@2x"] tag:]; UIViewController *fifthVC = [[UIViewController alloc] init];
fifthVC.view.backgroundColor = [UIColor greenColor]; fifthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:]; UIViewController *sixVC = [[UIViewController alloc] init];
sixVC.view.backgroundColor = [UIColor greenColor]; sixVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:]; // 设置控制器数组
mainTabBar.viewControllers = @[firstVC, secondVC, thirdVC, fourthVC, fifthVC, sixVC];
// [mainTabBar addChildViewController:firstVC];
// [mainTabBar addChildViewController:secondVC]; // 3.将UITabBarController对象设置为window的根视图控制器
self.window.rootViewController = mainTabBar; // 设置进入应用选中第几个
mainTabBar.selectedIndex = ; #pragma mark - TabBar的属性
// 修改字体图标等的选中颜色
mainTabBar.tabBar.tintColor = [UIColor greenColor]; // 设置tabBar的半透明与否
mainTabBar.tabBar.translucent = NO; // 设置tabBar的颜色
mainTabBar.tabBar.barTintColor = [UIColor purpleColor]; // 改变tabBar的位置
[secondVC.tabBarItem setTitlePositionAdjustment:UIOffsetMake(, )]; // 设置消息提示
thirdVC.tabBarItem.badgeValue = @"99+"; // 设置代理
mainTabBar.delegate = self; return YES;
} #pragma mark - 实现代理方法
// 清除提示消息
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { viewController.tabBarItem.badgeValue = nil;
} @end