#####iOS初学者 废话不多说 直接上代码,文中看注释
-
先创建三个tabBar页
-
在创建tabBar页面
- 在创建几张tabBar的图片
-
main.m文件
#import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char * argv[]) { NSString * appDelegateClassName; @autoreleasepool { // Setup code that might create autoreleased objects goes here. appDelegateClassName = NSStringFromClass([AppDelegate class]); } return UIApplicationMain(argc, argv, nil, appDelegateClassName); } -
AppDelegate.m
#import "AppDelegate.h" #import "MyTabBarController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor whiteColor]; MyTabBarController *rootViewContoller = [[MyTabBarController alloc] init]; self.window.rootViewController =rootViewContoller; [self.window makeKeyAndVisible]; return YES; } @end -
AppDelegate.h
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end -
MyTabBarController.m
#import "MyTabBarController.h" #import "TabOneViewController.h" #import "TabTwoViewController.h" #import "TabThreeViewController.h" @interface MyTabBarController () @end @implementation MyTabBarController - (void)viewDidLoad { [super viewDidLoad]; /*搭建tabBar结构控制器*/ [self configureViewControllers]; //自定义TabBar事件 [self addOnCLick]; } - (void)configureViewControllers { //为三个tabBar页分配内存 和 初始化 TabOneViewController *TabOneVC = [[TabOneViewController alloc]init]; TabTwoViewController *TabTwoVC = [[TabTwoViewController alloc]init]; TabThreeViewController *TabThreeVC = [[TabThreeViewController alloc]init]; // 建立空间大的一个可变数组 NSArray *vcs = @[TabOneVC,TabTwoVC,TabThreeVC]; NSArray *itemTitle = @[@"tab1",@"tab2",@"tab3"]; //定义他tabBar的文字 NSArray *itemImage = @[@"home",@"tab2",@"tab3",]; //未选中的转态图 NSArray *itemImageActive = @[@"home_active",@"tab2_active",@"tab3_active"];//选中的转态的图 NSMutableArray *navs = [NSMutableArray arrayWithCapacity:vcs.count]; // 循环控制 for (int i=0; i<[vcs count]; i++) { UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:vcs[i]]; //未选择的状态图 navigation.tabBarItem.image = [UIImage imageNamed:itemImage[i]]; //选中的状态图 UIImage *tabBarSelectedImage = [UIImage imageNamed:itemImageActive[i]]; tabBarSelectedImage = [tabBarSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [navigation.tabBarItem setSelectedImage:tabBarSelectedImage]; //设置tabBar字体 navigation.tabBarItem.title =itemTitle[i]; [navs addObject:navigation]; } //设置字体颜色主题 UITabBar *tabBar = [UITabBar appearance]; [tabBar setTintColor:UIColor.blueColor]; self.viewControllers = 和 初始化navs; } //tabBar的点击事件 - (void)tabBarButtonClick:(UIControl *)tabBarBtn{ NSLog(@"subView : %i",tabBarBtn.tag); } //创建点击tabBar的点击事件 -(void)addOnCLick{ int index = 0; for (UIButton * subView in self.tabBar.subviews) { NSLog(@"subViews : %@",subView); if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) { subView.tag = index; index++; //监听tabbar的点击 [subView addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside]; } } } @end -
MyTabBarController.h
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface MyTabBarController : UITabBarController @end NS_ASSUME_NONNULL_END
好了做完以上步骤就可以看到效果了
!