iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar 的解决办法

当在iOS应用中自定义tabbar后,使用popToRootViewController或popViewControllerAnimated方法可能会出现两个tabbar。为解决此问题,可以在pop时发送通知,并在自定义tabbar的viewDidLoad中注册接收通知,从而在接收到通知时调用removeTabBarButton方法删除系统自带的tabbar。这种方法在连续调用popViewController方法时也能正常工作,表明popViewController与popToViewController在处理自定义tabbar时的行为存在差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS自定义tabbarpopToRootViewContrillerpoptoviewcontroller时出现两个tabbar 的解决办法


问题:iOS自定义tabbarpopToRootViewContrillerpoptoviewcontroller时出现两个tabbar

1.自定义代码:

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    

    // 删除系统自动生成的UITabBarButton

    [self removeTabBarButton];

}


-(void) removeTabBarButton {

    // 删除系统自动生成的UITabBarButton

    for (UIView *child in self.tabBar.subviews) {

        if ([child isKindOfClass:[UIControl class]]) {

            [child removeFromSuperview];

        }

    }

}


/**

 *  初始化tabbar

 */

- (void)setupTabbar

{

    HYTTabBar *customTabBar = [[HYTTabBar alloc] init];

    customTabBar.frame = self.tabBar.bounds;

    customTabBar.delegate = self;

    [self.tabBar addSubview:customTabBar];

    self.customTabBar = customTabBar;

}


2.pop代码:

[self.navigationController popToViewController:strongSelf.navigationController.childViewControllers[1] animated:YES];


3.结果:(因问题已经解决,暂时从网上找了一张遇到同样问题的图片作为替代)

21_88419_b6e87b66aee7f8b.png


解决方法:

1. pop的时候 发送通知

#define HYTNotificationCenter [NSNotificationCenter defaultCenter]

[HYTNotificationCenter postNotificationName:HYTPopViewControllerNotification object:nil];


2. 在自定义的tabcontroller viewdidload方法中注册通知,调用removeTabBarButton方法删除系统自带的就可以了

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 初始化tabbar

    [self setupTabbar];

    

    //.../


    [HYTNotificationCenter addObserver:self selector:@selector(removeTabBarButton) name:HYTPopViewControllerNotification object:nil];

}


-(void) removeTabBarButton {

    // 删除系统自动生成的UITabBarButton

    for (UIView *child in self.tabBar.subviews) {

        if ([child isKindOfClass:[UIControl class]]) {

            [child removeFromSuperview];

        }

    }

}


ps:我尝试过连续调用几个popviewcontroller的方法来替代poptoviewcontroller,结果正常。

这说明popviewcontroller poptoviewcontroller 的实现至少在自定义tabbar上是有本质差别的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值