• iOS之定制tabbar


    我们知道,一个Tab控制器控制着若干视图控制器,它是由一个数组进行管理的,每一个Tab控制器只有一 UITabBar视图,用于显示UITabBarItem实例。我们通过点击UITabBarItem来切换视图控制器,现在我们就来说怎么定制我们想要的tabbar视图。
    步骤一:通过Single ViewApplication取新建一个工程,在Main.storyboard中右边栏中选择TabBarController拖进storyboard操作区间,如图:

      步骤二:我们把TabBarViewController连着的两个itemViewController删除,重新拖两个NavigationViewController,并通过vireControllers连线,结果如下:
    

    在左边选择对应的控制器的item,在右边的TabBarItem和BarItem中填写相应的信息:

    同理,如果我们需要更多的barItem,重复步骤二便是,即我们需要几个baritem,就拖几个NavigationViewController,再执行相同的操作。
    就比如我们要四个baritem,并且在storyboard中的baritem中设置对应的图片后,得到这样的效果:

    但是,如果我们不想要item下面的标题,去掉标题后问题来了:item图标明显偏上:

    怎么让这些item居中呢?
    我们在这里调整:

    将Image Inset中的Top和Bottom由原来的0分别改成5,-5即可。
    于是得到:

    显然,这还不是我们最终要的,我们还需要点击效果:有item的切换图标 ,并且有选中效果,我们在AppDelegate中加入如下代码即可:
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
    UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
    tabBarItem1.selectedImage = [UIImage imageNamed:@"hot_selected"];
    tabBarItem2.selectedImage = [UIImage imageNamed:@"collect_selected"];
    tabBarItem3.selectedImage = [UIImage imageNamed:@"category_selected"];
    tabBarItem4.selectedImage =[UIImage imageNamed:@"me_selected"];
    //设置选中item的图片颜⾊
    [tabBarController.tabBar setSelectedImageTintColor:[UIColor whiteColor]];
    //设置选中item后,显示在此item下面的图⽚
    tabBar.selectionIndicatorImage = [UIImage imageNamed:@"select_bg"];
    关于taabr,我们还有这样的设置:
    //设置tabBar的背景图⽚
    tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbarbg.png"];
    //设置tabBar的背景颜⾊
    tabBarController.tabBar.tintColor = [UIColor grayColor];

  • 相关阅读:
    Fluentd部署:如何监控Fluentd
    【615】国内国外经纬度坐标转换
    【614】矢量数据转栅格数据(cv2.fillPoly/cv2.polylines)
    【613】U-Net 相关
    【612】深度学习模型相关问题
    【611】keras 后端 backend 相关函数(Dice实现)
    面试官:new 关键字在 JVM 中是如何执行的?
    IntelliJ IDEA 2021.2 发布,这次要干掉 FindBugs 了!!
    Nginx 实现 10w+ 并发之 Linux 内核优化
    我们真的需要全栈开发吗?
  • 原文地址:https://www.cnblogs.com/shanpow/p/4137335.html
Copyright © 2020-2023  润新知