美文网首页
UITabBarItem的image/selectedImage

UITabBarItem的image/selectedImage

作者: tp夕阳武士 | 来源:发表于2017-08-23 11:30 被阅读40次

    国内应用的比较流行风格是由一个UITabBarController来控制不同的板块。很多应用因为营销的关系需要随着各个时间段的不同而调整UITabBarController里面的各个Controller对应的tabBarItem的image/selectedImage/title。例如:饿了么,淘宝,优酷....

    实现如下:

    1.创建一个继承UITabBarController的子类->>RFTabBarController;

    2.在RFTabBarController的.m文件中重写方法- (void)viewDidLoad;
    在- (void)viewDidLoad;中初始化它管理的各个控制器对应的tabBarItem。

    3.初始化要用下面这个方法:

    UITabBarItem *item1 = [[UITabBarItem alloc]
    initWithTitle:@"VC1" image:[[UIImage imageNamed:@"VC1_icon"] 
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 
    selectedImage:[[UIImage imageNamed:@"VC1_seleted_icon"] 
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    

    然后用同样的方法初始化其他几个控制器的tabBarItem;

    1. 当需要修改tabBarItem的image/selectedImage/title时,这些材料从哪里来?

    有些应用在发布之前已经计划好未来会使用到什么样的图片,他们会直接把文件放到项目的图片文件夹中,但是更多的情况是,应用的运营者并不知道未来会使用到什么图片和标题。

    这个时候就需要结合服务器,当应用需要更改tabBarItem的image/selectedImage/title时,服务器就发一个包给客户端,客户端收到包后,读取其中配置文件的数据set到一个自定义的专门用于配置UITabBarController的各个控制器对应的tabBarItem的image/selectedImage/title属性的类中。

    例如可以封装一个模型类:RFTabbarItemsModel

    
    /**标签栏背景图片宽度:320*/
    @property (nonatomic,strong) NSString *tabbarBgImage320Path;
    /**标签栏背景图片宽度:375*/
    @property (nonatomic,strong) NSString *tabbarBgImage375Path;
    /**标签栏背景图片宽度:540*/
    @property (nonatomic,strong) NSString *tabbarBgImage540Path;
    
    @property (nonatomic,strong) NSString *kuControlImagePath;
    @property (nonatomic,strong) NSString *kuControlUnselectImagePath;
    
    @property (nonatomic,strong) NSString *serviceImagePath;
    @property (nonatomic,strong) NSString *serviceUnselectImagePath;
    
    @property (nonatomic,strong) NSString *findImagePath;
    @property (nonatomic,strong) NSString *findUnselectImagePath;
    
    @property (nonatomic,strong) NSString *mineImagePath;
    @property (nonatomic,strong) NSString *mineUnselectImagePath;
    
    /**四个标签的标题*/
    @property (nonatomic,strong) NSString *title1;
    @property (nonatomic,strong) NSString *title2;
    @property (nonatomic,strong) NSString *title3;
    @property (nonatomic,strong) NSString *title4;
    

    5.在RFTabBarController的.m文件中声明一个用户修改tabBarItem的image/selectedImage/title的方法

    #pragma mark 监听通知修改tabbar的样式
    -(void)changeTabbarStyle{
        
        RFTabbarItemsModel *skinTemplate = [RFTabbarItemsModel sharedRFTabbarItemsModel];
        if (VIEW_BOUNDS.width == 320) {
            //se屏幕
            self.tabBar.backgroundImage = [UIImage imageWithContentsOfFile:skinTemplate.tabbarBgImage320Path];
        }else if (VIEW_BOUNDS.width == 375){
            //iphone6屏幕
            self.tabBar.backgroundImage = [UIImage imageWithContentsOfFile:skinTemplate.tabbarBgImage375Path];
        }else{
            self.tabBar.backgroundImage = [UIImage imageWithContentsOfFile:skinTemplate.tabbarBgImage540Path];
        }
        
        self.tabBar.tintColor = [UIColor colorWithRed:skinTemplate.tabbarSelectColorR green:skinTemplate.tabbarSelectColorG blue:skinTemplate.tabbarSelectColorB alpha:1];
        
    
        self.yunkongzhiItem.image = [[UIImage imageWithContentsOfFile:skinTemplate.kuControlUnselectImagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.yunkongzhiItem.selectedImage = [[UIImage imageWithContentsOfFile:skinTemplate.kuControlImagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.yunkongzhiItem.title = skinTemplate.title1;
        
        self.carItem.image = [[UIImage imageWithContentsOfFile:skinTemplate.serviceUnselectImagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.carItem.selectedImage = [[UIImage imageWithContentsOfFile:skinTemplate.serviceImagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.carItem.title = skinTemplate.title2;
        
        self.infoItem.image = [[UIImage imageWithContentsOfFile:skinTemplate.findUnselectImagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.infoItem.selectedImage = [[UIImage imageWithContentsOfFile:skinTemplate.findImagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.infoItem.title = skinTemplate.title3;
        
        self.mineItem.image = [[UIImage imageWithContentsOfFile:skinTemplate.mineUnselectImagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.mineItem.selectedImage = [[UIImage imageWithContentsOfFile:skinTemplate.mineImagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.mineItem.title = skinTemplate.title4;
    
    }
    

    相关文章

      网友评论

          本文标题:UITabBarItem的image/selectedImage

          本文链接:https://www.haomeiwen.com/subject/rvgvkxtx.html