iOS自訂tabbar後popToRootViewContriller和poptoviewcontroller時出現兩個tabbar 的解決辦法

來源:互聯網
上載者:User

標籤:

iOS自訂tabbar後popToRootViewContriller和poptoviewcontroller時出現兩個tabbar  的解決辦法

問題:iOS自訂tabbar後popToRootViewContriller和poptoviewcontroller時出現兩個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.結果:

 

 

解決方案:

1. pop的時候  發送通知(注意是從  要pop回帶有tabber的那個VC控制器發出通知)

 NSNotification *notification =[NSNotification notificationWithName:@"HYTPopViewControllerNotification" object:nil userInfo:nil];

  [[NSNotificationCenter defaultCenter] postNotification:notification];

 

 

2. 在自訂的tabcontroller 的viewdidload方法中註冊通知,調用removeTabBarButton方法刪除系統內建的就可以了

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 初始化tabbar

    [self setupTabbar];

    

    //.../

 

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HYTPopViewControllerNotification) name:@"HYTPopViewControllerNotification" object:nil];

}

 

-(void) HYTPopViewControllerNotification {

    // 刪除系統自動產生的UITabBarButton

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

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

            [child  removeFromSuperview];

        }

    }

}

ps:我嘗試過連續調用幾個popviewcontroller的方法來替代poptoviewcontroller,結果正常。

這說明popviewcontroller 和 poptoviewcontroller 的實現至少在自訂tabbar上是有本質差別的。

iOS自訂tabbar後popToRootViewContriller和poptoviewcontroller時出現兩個tabbar 的解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.