UITabBarController的使用自定义UITabBar样式

本文介绍了如何在使用UITabBarController时自定义UITabBar的样式,提供了两种实现方法:方法1通过在TabBarController的view中添加自定义子视图并设置响应方法;方法2通过继承UITabBarController并在其中设置样式。每种方法都有相应的代码示例和效果图展示。
摘要由CSDN通过智能技术生成

UITabBarController的使用过程中,可能会出现需要自定义UITabBar样式的时候,下面介绍两种实现方法。

方法1
存在N个视图控制器,且需要使用自定义的UITabBar样式来进行交互操作哪个视图控制器的显示。具体做法是在UITabBarController中的view中添加自定义样式的子视图,设置子视图响应方法跳转对应的视图控制器,然后再通过UITabBarController的代理方法重置自定义样式的子视图状态。

代码示例,继承UITabBarController的方式实现

#import "BaseTabBarController.h"
#import "MainVC.h"
#import "MineVC.h"
#import "AddVC.h"

@interface BaseTabBarController () <UITabBarControllerDelegate>

@property (nonatomic, strong) UIButton *addButton;

@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initlizeAddButton];
}

#pragma mark - 实现

- (void)initlizeAddButton
{
    self.delegate = self;
    [self addChildVc:[[MainVC alloc] init] title:@"首页" image:@"homeNormal" selectedImage:@"homeSelected"];
    [self addChildVc:[[AddVC alloc] init] title:nil image:nil selectedImage:nil];
    [self addChildVc:[[MineVC alloc] init] title:@"我的" image:@"mineNormal" selectedImage:@"mineSelected"];

    self.tabBar.backgroundColor = [UIColor yellowColor];
    
    self.addButton = [[UIButton alloc] initWithFrame:CGRectMake((self.tabBar.frame.size.width - 70.0) / 2, (self.view.frame.size.height - 70.0) - 44.0, 70.0, 70.0)];
    self.addButton.backgroundColor = [UIColor yellowColor];
    self.addButton.layer.cornerRadius = 35;
    self.addButton.layer.masksToBounds = YES;
    [self.addButton setImage:[UIImage imageNamed:@"addNormal"] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"addSelected"] forState:UIControlStateSelected];
    [self.addButton addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.addButton];
    [self.view bringSubviewToFront:self.addButton];
}

- (void)addClick:(UIButton *)button
{
    button.selected = YES;
    button.userInteractionEnabled = NO;
    self.selectedIndex = 1;
}

- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字(可以设置tabBar和navigationBar的文字)
    if (title) {
        childVc.title = title;
    }
    
    // 设置子控制器的tabBa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值