自定义 TabBar 丢失 TabBarItem 图像 [英] custom TabBar loses TabBarItem images

查看:28
本文介绍了自定义 TabBar 丢失 TabBarItem 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个运行良好的自定义 UITabBar,但我对导航进行了更改,现在我遇到了一些严重的问题.这是我所拥有的:

I have made a custom UITabBar that is working very well, but I introduced a change to navigation and now i am having some serious issues. Here is what I have:

常规设置

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 方法,如下所示

App Delegate - In my didFinishLaunchingWithOptions method I call a customizeTabBar method as follows

-(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];
}

到目前为止,这一切都很好.

So far this is working beautifully.

这里是问题所在.为了让我的 TabBarItem3 链接到 ContentView,我在 TabBarClass 中实现了以下代码:

Here is where the problem comes in. In order to get my TabBarItem3 to link to ContentView, I implemented the following code in my 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(取决于我选择的项目)的自定义标签栏相关联的图像.

Upon execution of this code, I lose the images associated with my custom tab bar for items 2 or 3 (depending on which I select).

更新

因此,我将我的标签栏自定义方法从我的委托中移到了 maintabbar 类中,并在 viewdidload 上调用了它,我的 meh hid didselectviewcontroller.这似乎解决了丢失图像的问题,但是当我单击任何一个软管项目时,它会在屏幕上产生闪烁的不良副作用.我已经尝试了从 viewdidload 方法中删除它的不同组合,但仍然没有运气..

So, I moved my tab bar customization method out of my delegate and into a maintabbar class and I call it on viewdidload and I the meh hid didselectviewcontroller. This seems to solve the problem with missing images, but has created a bad side effect of flickering on the screen when I click either of hose items. I have tried different combinations of removing it from the viewdidload method as we'll, but still no luck..

推荐答案

据我所知,整个事情的目的是避免必须为两个标签栏项目创建不同的类,这两个标签栏项目将执行几乎相同的事情但具有不同的数据.

As I understand the purpose of this whole thing is to avoid having to create different classes for two tab bar items that will perform almost the same thing but with different data.

所以拳头,你必须定义一个能够做这种事情的类,我们称之为CommonClass.这个类必须有必要的方法来进行正确的初始化.[我确定你已经这样做了].

So fist, you have to defined a class capable of doing such thing, let's call it CommonClass. This class will have to have the necessary methods for doing a properly initialization. [I'm sure you have done this].

然后在你的故事板上,你仍然需要有两个 ViewController 元素,每个元素作为它对应的 NavigationController 的 rootViewController.

Then on your storyboard, you will still need to have two ViewController elements, each one as rootViewController of it's corresponding NavigationController.

我强烈建议这样做,而不是像我们尝试的那样尝试通过代码分配新实例:从 Tabar 多次链接到视图.这两个视图控制器显然属于同一个类 CommonClass 所以你仍然会重用那个类.这样我们就不必在执行期间更改 NavigationBar ,这可能会很混乱.

I strongly recommend doing this way instead of trying to assign a new instance by code as we tried on: Linking to a view multiple times from tabar. Those two view controllers obviously belong to the same class CommonClass so you will still be reusing that class. In this way we won't have to alter the NavigationBar during the execution time which can be messy.

然后,您可以为您的 CommonClass 发送必要的初始化信息,在您的 CommonClass 上有一个方法,该方法将根据选定的 tabBarItem 加载适当的信息/数据,您可以在 viewDidLoad 方法上做到这一点.

Then, you can send the necessary initialization information for your CommonClass having a method on your CommonClass that will load the appropiate information/data according to the selected tabBarItem, you can do that on the viewDidLoad method.

如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (self.tabBarController.selectedIndex == 1) {
        [self customizeWithData:@"Specific data for my first instance"];
    } else if (self.tabBarController.selectedIndex == 2) {
        [self customizeWithData:@"Specific data for my second instance"];        
    }
}

通过这种方式,您将只在代码的一部分中自定义标签栏,并且由于您不会在执行时更改它,因此您不需要在其他部分进行自定义导致闪烁.

In this way you will be doing the customization of your tab bar in just one part of your code and as you won't be altering it in execution time you won't need to customization in other parts causing flickering.

这篇关于自定义 TabBar 丢失 TabBarItem 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆