欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UITabBarController与UINavigationController混合开发

程序员文章站 2022-04-01 12:48:35
...

这种方式可以让你快速生成一个UITabBarController与UINavigationController的混合开发形式!

首先我们在入口类指定TabBarController
#import"AppDelegate.h"

#import"QWTabBarController.h"

@interfaceAppDelegate()

@end

@implementationAppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

self.window= [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

QWTabBarController*tabBar = [[QWTabBarControlleralloc]init];

self.window.rootViewController= tabBar;

[self.windowmakeKeyAndVisible];

returnYES;

}
2.创建一个UINavigationController
.h

#[email protected] QWNavitionController: UINavigationController

@end

.m

#import "QWNavitionController.h"

@interface QWNavitionController ()

@end

@implementation QWNavitionController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationBar.barTintColor = MAIN_COLOR;
    
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20.0]}];
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
    
}

@end
3.创建对应的UITabBarController
.h

#[email protected] QWTabBarController: UITabBarController

@end

.m

#import "QWTabBarController.h"

#import "QWNavitionController.h"

@interface QWTabBarController ()

@end

@implementation QWTabBarController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addChildViewControllers];
    
}

- (void)addChildViewControllers

{
    
#warning  添加 被TabBarCtl 控制的视图控制器 名称
    
    //视图控制器名称  按照你自己所管理的控制器来定义
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"QWBaseViewController",@"QWBaseViewController",@"QWBaseViewController",@"QWBaseViewController"]];
    
#warning Set TabBarItem Nomal Icon Name
    
    //设置对应tabbar图片
    
    NSArray *imgArray = @[@"TabBar_home_23x23_",@"TabBar_gift_23x23_",@"TabBar_category_23x23_",@"TabBar_me_boy_23x23_"];
    
#warning Set TabBarItem selected Icon Name
    
    //设置对应tabbar选中图片
    
    NSArray *selectImageArray = @[@"TabBar_home_23x23_selected",@"TabBar_gift_23x23_selected",@"TabBar_category_23x23_selected",@"TabBar_me_boy_23x23_selected"];
    
#warning Set TabBarItem title
    
    NSArray *titles = @[@"test1",@"test2",@"test3",@"test4"];
    
    for(int i =0;i<array.count;i++)
        
    {
        
        //使用NSClassFromString来进行不确定的类进行初始化。
        
        UIViewController *vc = [[NSClassFromString(array[i]) alloc] init];
        
        QWNavitionController *nav = [[QWNavitionController alloc] initWithRootViewController:vc];
        
        vc.title = titles[i];
        
        nav.tabBarItem.title = titles[i];
        
        nav.tabBarItem.image = IMG(imgArray[i]);
        
        nav.tabBarItem.selectedImage = [IMG(selectImageArray[i]) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        [array replaceObjectAtIndex:i withObject:nav];
        
    }
    
    self.viewControllers = array;
    
    self.tabBar.tintColor = MAIN_COLOR;
    
 - (void)didReceiveMemoryWarning {
        
     [super didReceiveMemoryWarning];
        
 }
@end