• iOS-UITabBar的使用包括选中前后使用不同的自定义颜色以及图标颜色的改变问题)imageView,titles


       1.先上传出现的效果图

       2.UItabbar的具体属性我不接扫了,网络上有好多博文,我只说这个tabBar的效果我是怎么实现的;

       3.TabBar自带属性中是字体未选中的时候为黑色,选中的时候为蓝色,这个颜色都是tabBar自己处理过的,所以在实际操作中会发现黑色没有那么的黑,会有一点灰色的感觉;

       4.贴代码,这段代码实现的功能是选中前为灰色,选中后为红色,这个使用于字体颜色的变化

        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

        attrs[NSForegroundColorAttributeName] = [UIColor grayColor];    

        // 选中

        NSMutableDictionary *attrSelected = [NSMutableDictionary dictionary];

        attrSelected[NSForegroundColorAttributeName] = [UIColor redColor];

        UITabBarItem *item = [UITabBarItem appearance];

        [item setTitleTextAttributes:attrs forState:UIControlStateNormal];

        [item setTitleTextAttributes:attrSelected forState:UIControlStateSelected];

       5.关于图片颜色的变化,会有作者写道直接定义图片选中颜色为xxx,我没有实践过,不过喜欢的人可以用一下,我的思路是选取两张图片,在未选中的情况下使用一张,在选中的情况下使用另一张,具体代码如下

            UIImage *image = [[UIImage imageNamed:@"首页图标"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

            UIImage *image1 = [[UIImage imageNamed:@"首页粉"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

            [self.tabBarItem setImage:image];

            self.tabBarItem.selectedImage = image1;

        6. 之前还说到想自定义tabBar的颜色的问题,代码如下

        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 49)];

        backView.backgroundColor = [UIColor whiteColor];

        backView.layer.borderColor = [UIColor colorWithHexString:@"#D7D7D7" alpha:0.9].CGColor;

        backView.layer.borderWidth = 1.0f;

        [self.tabBarController.tabBar insertSubview:backView atIndex:0];

        这个颜色就不会被tabBar的自定义处理,就是自己看到的颜色,适合做彩色的tabBar。

        7.从我图标的命名也可以看出来,粉色的图片就是我选中以后出现的图片,这样就可以出来这个效果了,我现在正在做一个完整的APP进行练手,里边会有这个TabBar的全部代码,如果感觉还是看不明白的同学,可以进入我的github进行代码下载,或者联系我的QQ,代码理由,欢迎点星,支持妹子,我的github地址是https://github.com/leroypus

  • 相关阅读:
    链表重排 【模板题】
    链表的插入排序
    链表归并排序
    判断链表成环| 删除第K个节点【快慢指针】
    vue骨架屏制作
    前端常用的网站+插件
    点击canvas图片下载图片
    判断dom是否出现在可视区域
    canvas截取图片
    .如果在input上加上@keyup.enter.native,第一次回车时还是会刷新界面,在el-from上加上 @submit.native.prevent
  • 原文地址:https://www.cnblogs.com/lepus/p/5919587.html
Copyright © 2020-2023  润新知