iOS自定義tabbar後popToRootViewContriller和poptoviewcontroller時出現兩個tabbar 的解決辦法
iOS自定義tabbar後popToRootViewContriller和poptoviewcontroller時出現兩個tabbar 的解決辦法
問題:iOS自定義tabbar後popToRootViewContriller和poptoviewcontroller時出現兩個tabbar
1.自定義程式碼:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// 刪除系統自動生成的UITabBarButton
[self removeTabBarButton];
}
-(void
// 刪除系統自動生成的UITabBarButton
for (UIView *child in self.tabBar.subviews) {
if ([child isKindOfClass:[UIControl class]]) {
[child removeFromSuperview];
}
}
}
/**
* 初始化tabbar
*/
- (void)setupTabbar
{
HYTTabBar *customTabBar = [[HYTTabBar
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
3.結果:(因問題已經解決,暫時從網上找了一張遇到同樣問題的圖片作為替代)
解決方法:
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上是有本質差別的。