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

UITabBarController和UINavigationController的自定义

程序员文章站 2022-04-01 12:41:28
...

UITabBarController和UINavigationController的自定义

选项卡控制器( UITabBarController )和导航栏控制器( UINavigationController )可以说是每个 APP 中都会用到的组件,但是由于 iOS SDK 已经有了一定的定制,所以往往当我们使用的时候,会对其进行修改。

UITabBarController

首先我们要知道在UITabBarController中我们要做些什么:

  • UITabBarController最重要也是最核心的功能:装载选项卡
  • 对UITabBar样式的修改:背景色、透明、分割线
  • 对TabBarItem的修改:字体、颜色、图片渲染

现在先来看第一个,装载选项卡。要知道每一个 UIViewController 装进 TabBar 一般都要设置 title、默认图片、选中图片以及装载 NavigationController 那么将这些操作统一封装一下:

- (void)addSubviewController:(UIViewController *)childViewController title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    
    //设置tabBarItem的标题
    childViewController.title = title;
    
    if (image != nil || selectedImage != nil) {
        //设置tabBarItem的图片
        childViewController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //设置tabbaritem选中时的显示的图片
        childViewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    //实例化一个VC为根视图的NavigationViewController
    HQNavigationController *navigationVc = [[HQNavigationController alloc] initWithRootViewController:childViewController];
    
    //添加MyNavigationViewController到tabbar
    [self addChildViewController:navigationVc];
    
}

接下来设置TabBar的样式以及TabBarItem的样式。

+ (void)setupTabBarTheme {
    
    UITabBar *bar = [UITabBar appearance];
    
    bar.translucent = NO;
    
    //设置分割线
    bar.shadowImage = [UIImage imageWithColor:UIColorFromRGB(0xf1f1f1) size:CGSizeMake(ScreenWidth, 0.5)];
    //设置背景图
    [bar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(ScreenWidth, 49)]];
    
    
}
+ (void)setupTabBarItemTheme {
    
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    
    /****设置文字属性****/
    //普通状态颜色,字体大小
    [tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName : UIColorFromRGB(0x949494)} forState:UIControlStateNormal];
    
    //选中状态颜色,字体大小
    [tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName : UIColorFromRGB(0xff5438)} forState:UIControlStateSelected];
    
    //高亮状态
//    [tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f]} forState:UIControlStateHighlighted];
    
    //不可用状态
//    [tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f]} forState:UIControlStateDisabled];
    
}

这样便完成了UITabBarController的定制.

UINavigationController

同样,先看看要设置什么东西:

  • 自定义后首先加上系统默认侧滑返回手势。
  • 更具需要重写 push、pop 方法.
  • 定制NavigationBar样式
  • 定制NavigationBarItem样式
#import "HQNavigationController.h"

@interface HQNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation HQNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.interactivePopGestureRecognizer.delegate = self;
    
}

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

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

#pragma mark - 设置导航栏主题
+ (void)setupNavigationBarTheme {
    
    UINavigationBar *bar = [UINavigationBar appearance];
    
    bar.translucent = NO;
    
    //设置导航栏的title属性
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0x3a3a3a),NSFontAttributeName : [UIFont defaultFontOfSize:18]}];
    
    //设置分割线
    bar.shadowImage = [UIImage imageWithColor:UIColorFromRGB(0xf1f1f1) size:CGSizeMake(ScreenWidth, 0.5)];
    
    //设置导航栏背景图
    [bar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(ScreenWidth, 49)] 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:animated];
}


/**
 *  点击返回按钮时调用
 *  返回上一个界面
 */
-(void)back {
    
    [super popViewControllerAnimated:YES];
    
}

/**
 *  手势识别器对象会调用这个代理方法来决定手势是否有效
 *
 *  @return YES : 手势有效, NO : 手势无效
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    // 手势何时有效 : 当导航控制器的子控制器个数 > 1就有效
    return self.childViewControllers.count > 1;
    
}