关于简单的自定义NavigationController和TabBarController

本文写的是关于基础的模板样式,TabBar上有四个item,然后绑定NavigationController,在具体的实现过程中可能会有不同的样式和需求,所以需要看情况更改。

首先封装NavigationController

新建一个HHBaseNavigationController,继承自UINavigationController
然后在其.m文件中设置


+ (void)initialize {
    // 设置导航items数据主题
    [self setupNavigationItemsTheme];
    
    // 设置导航栏主题
    [self setupNavigationBarTheme];
}


#pragma mark -  设置导航items数据主题
+ (void)setupNavigationItemsTheme {
    UIBarButtonItem *barButtonItem = [UIBarButtonItem appearance];
    // 设置字体颜色
    [barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:14]} forState:UIControlStateNormal];
    [barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateHighlighted];
    [barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor]} forState:UIControlStateDisabled];
    
}

#pragma mark -  设置导航栏主题
+ (void)setupNavigationBarTheme {
    UINavigationBar * navBar = [UINavigationBar appearance];
    
    // 设置导航栏title属性
    [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
    // 设置导航栏颜色
    [navBar setBarTintColor:[UIColor cyanColor]];
    
    UIImage *image = [UIImage imageNamed:@"nav_64"];
    
    [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

}


#pragma mark -  拦截所有push方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.viewControllers.count > 0) {
        // 如果navigationController的字控制器个数大于两个就隐藏
        viewController.hidesBottomBarWhenPushed = YES;
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    }
    [super pushViewController:viewController animated:YES];
}

#pragma mark -  拦截所有pop方法
- (void)back {
    [super popViewControllerAnimated:YES];
     //这里就可以自行修改返回按钮的各种属性等
}


再封装一个TabBarController

新建一个HHTabBarViewController,继承自UITabBarController
然后在其.m文件中设置

//首先就是包含NavigationController的头文件
#import "HHBaseNavigationController.h"

#import "XBSViewController.h" //这是我要绑定的几个主页面
#import "SearchViewController.h" //这是我要绑定的几个主页面
#import "HomeViewController.h"//这是我要绑定的几个主页面
#import "MeViewController.h"// 这是我要绑定的几个主页面

+ (void)initialize {
    
    // 设置UITabBarItem主题
    [self setupTabBarItemTheme];
    
    // 设置UITabBar主题
    [self setupTabBarTheme];
}

+ (void)setupTabBarItemTheme {
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    
    /**设置文字属性**/
    // 普通状态
    [tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f], NSForegroundColorAttributeName : [UIColor grayColor]} forState:UIControlStateNormal];
    
    // 选中状态
    [tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    
    // 高亮状态
    //    [tabBarItem setTitleTextAttributes:@{} forState:UIControlStateHighlighted];
    
    // 不可用状态(disable)
    //    [tabBarItem setTitleTextAttributes:@{} forState:UIControlStateDisabled];
}

+ (void)setupTabBarTheme {
    
    //    UITabBar *tabBar = [UITabBar appearance];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加所有子控制器
    [self addAllViewControllers];
    
    // 创建自定义TabBar
    [self addCustomTabBar];
}

#pragma mark - 添加所有子控制器
- (void)addAllViewControllers {
   //将需要绑定的页面添加进来
    XBSViewController *XBSVc = [XBSViewController new];
    [self addOneChildVc:XBSVc title:@"幼儿园" imageName:@"app_icon02" selectedImageName:@"app_icon02_b"];
    
    SearchViewController *searchVc = [SearchViewController new];
    [self addOneChildVc:searchVc title:@"家庭教育" imageName:@"app_icon03" selectedImageName:@"app_icon03_b"];
    
    HomeViewController *homeVc = [HomeViewController new];
    [self addOneChildVc:homeVc title:@"发现" imageName:@"app_icon04" selectedImageName:@"app_icon04_b"];
    
    MeViewController *meVc = [MeViewController new];
    [self addOneChildVc:meVc title:@"我" imageName:@"app_icon01" selectedImageName:@"app_icon01_b"];
    
}

#pragma mark - 添加一个子控制器
- (void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)seletedImageName {
    
    childVc.tabBarItem.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:seletedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
//    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], NSForegroundColorAttributeName, [UIFont fontWithName:title size:12.0f],NSFontAttributeName,nil] forState:UIControlStateNormal];
//    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor], NSForegroundColorAttributeName, [UIFont fontWithName:title size:12.0f],NSFontAttributeName,nil] forState:UIControlStateSelected];
    
//将NavigationController给包含进来。
    [self addChildViewController:[[HHBaseNavigationController alloc] initWithRootViewController:childVc]];
}

#pragma mark - 自定义TabBar
- (void)addCustomTabBar {
    //    GLTabBar *tabBar = [GLTabBar new];
    //    tabBar.tabBarDelegate = self;
    //    [self setValue:tabBar forKeyPath:@"tabBar"];
}

最后就是在AppDelegate.m里边设置开始页面了
//包含刚才封装的两个头文件
#import "HHBaseNavigationViewController.h"
#import "HHTabBarViewController.h"


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    _window.backgroundColor = [UIColor whiteColor];
    
//    _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    _window.rootViewController = HHTabBarViewController.new;
    
    [_window makeKeyAndVisible];
    //设置状态栏
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    return YES;
}


以上就完成了最基本的设置

通过看这次简单的封装,我们能找到封装的思路,然后模仿着去做一些改动。
不过想要达到各种效果,就需要去进行一些符合自己要求的更改封装了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容