我制作了一个运行良好的自定义 UITabBar,但我对导航进行了更改,现在我遇到了一些严重的问题。这是我所拥有的:
常规设置
TabBarController
NavbarController1 - TabBarItem1
Links to: PeopleView
NavbarController2 - TabBarItem2
Links to: ContentView
NavbarController3 - TabBarItem3
Links to: ContentView //Same VC as TabBaritem 2.
App Delegate - 在我的 didFinishLaunchingWithOptions 方法中,我调用了 customizeTabBar 方法,如下所示
-(void) customizeTabBar
{
UITabBarController *tabVC = (UITabBarController *) self.window.rootViewController;
//Load Navigation Bar images
NSArray *unSelectedImages = [[NSArray alloc] initWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", nil];
NSArray *selectedImages = [[NSArray alloc] initWithObjects:@"image1_on.jpg", @"image2_on.jpg", @"image3_on.jpg", @"image4_on.jpg", @"image5_on.jpg", nil];
NSArray *items = tabVC.tabBar.items;
for (int idx = 0; idx < items.count; idx++)
{
UITabBarItem *barItem = [items objectAtIndex:idx];
barItem.tag = idx;
UIImage *selectedImage = [UIImage imageNamed:[selectedImages objectAtIndex:idx]];
UIImage *unSelectedImage = [UIImage imageNamed:[unSelectedImages objectAtIndex:idx]];
UIEdgeInsets inset = {
.top = 10,
.left = 0,
.bottom = -10,
.right = 0
};
barItem.imageInsets = inset;
[barItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unSelectedImage];
}
到目前为止,这工作得很好。
这就是问题所在。为了让我的 TabBarItem3 链接到 ContentView,我在 TabBarClass 中实现了以下代码:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController.tabBarItem.tag == 1 || viewController.tabBarItem.tag == 2 )
{
// Validating if is necesarry to replace the TabBarController.ViewControllers
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MediaList *mediaView = [storyboard instantiateViewControllerWithIdentifier:@"SB_MediaList"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mediaView];
NSMutableArray *viewsArray = [NSMutableArray arrayWithArray:self.viewControllers];
if (viewController.tabBarItem.tag == 1)
{
//Setting the specfic data for my instance for tabBarItem 1
NSLog(@"Here we are in 1");
[mediaView setContent:@"Personalized content/data for TabBarItem 1"];
}
else if (viewController.tabBarItem.tag == 2)
{
//Setting the specfic data for my instance for tabBarItem 2
NSLog(@"Here we are in 2");
[mediaView setContent:@"Personalized content/data for TabBarItem 2"];
}
[viewsArray replaceObjectAtIndex:viewController.tabBarItem.tag withObject:navigationController];
self.viewControllers = viewsArray;
}
}
执行此代码后,我会丢失与第 2 项或第 3 项的自定义选项卡栏关联的图像(取决于我选择的内容)。
更新
所以,我将我的标签栏自定义方法从我的委托中移到了一个 maintabbar 类中,我在 viewdidload 上调用它,然后我隐藏了 didselectviewcontroller。这似乎解决了丢失图像的问题,但是当我单击任一软管项目时,会产生屏幕闪烁的不良副作用。我已经尝试了从 viewdidload 方法中删除它的不同组合,但仍然没有运气..