UIKit框架-多控制器管理-TabBarController-标签控制器

TabBarController 标签控制器
效果图

这里写图片描述

1.将tabbarController设置成根视图控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];

    TabBarViewController *tabbar = [[TabBarViewController alloc] init];
    self.window.rootViewController = tabbar;
    return YES;
}
2. 初始化子控制器
- (void)viewDidLoad {
    [super viewDidLoad];

    // 替换tabbar
    [self setValue:[[TabBar alloc] init] forKey:@"tabBar"];

    // 初始化子控制器
    [self initChildViewController];
}

- (void)initChildViewController {
    ViewController *one = [[ViewController alloc] init];
    one.view.backgroundColor = [UIColor redColor];
    [self setupChildViewController:one title:@"首页" image:@"tabBar_essence_icon" highImage:@"tabBar_essence_click_icon"];

    ViewController *two = [[ViewController alloc] init];
    two.view.backgroundColor = [UIColor orangeColor];
    [self setupChildViewController:two title:@"朋友" image:@"tabBar_friendTrends_icon" highImage:@"tabBar_friendTrends_click_icon"];

    ViewController *three = [[ViewController alloc] init];
    three.view.backgroundColor = [UIColor yellowColor];
    [self setupChildViewController:three title:@"我" image:@"tabBar_me_icon" highImage:@"tabBar_me_click_icon"];

    ViewController *four = [[ViewController alloc] init];
    four.view.backgroundColor = [UIColor greenColor];
    [self setupChildViewController:four title:@"最新" image:@"tabBar_new_icon" highImage:@"tabBar_new_click_icon"];
}

- (void)setupChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image highImage:(NSString *)highImage {
    vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:image] selectedImage:[UIImage imageNamed:highImage]];
    [self addChildViewController:vc];
}
3. 改变底部的字体方法
+ (void)initialize {
    // 获取tabbarItem的外观
    UITabBarItem *appearance = [UITabBarItem appearance];
    // 添加字体属性
    [appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateNormal];
    [appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]} forState:UIControlStateSelected];
}
4. 更换自定义tabBar
    // 替换tabbar
    [self setValue:[[TabBar alloc] init] forKey:@"tabBar"];
5. 自定义tabbar (这里用到了UIView分类,点击查看分类代码
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.backgroundColor = [UIColor clearColor];
        [_button setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [_button setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [self addSubview:_button];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    // 设置自定义按钮的位子
    [_button setFrame:CGRectMake(self.frame.size.width*2/5, 0, self.frame.size.width/5, self.frame.size.height)];

    CGFloat w = self.frame.size.width / 5;
    // 重新布局tabbar自身的tabbarItem的位子
    NSInteger index = 0;
    for (UIControl *control in self.subviews) {
        if (![control isKindOfClass:[UIControl class]] || [control isKindOfClass:[UIButton class]]) continue;
        control.w = w;
        // 第一、二个不变,第三、四个变成第四、五个,所以索引+1
        control.x = index > 1 ? w * (index + 1) : w * index;
        index ++ ;
    }
}
6. 解决设置了图片,图片的颜色仍然是系统颜色

原因:图片被系统渲染
解决:使用原始图片

方法一:

这里写图片描述
选中图片
这里写图片描述
选择 Original Image
这里写图片描述

方法二 :
    UIImage *image = [UIImage imageNamed:@""];
    // 设置图片呈现模式
    [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
点击获取Demo示例

仅供参考,错误勿怪!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值