1
votes

I've been using a standard TabBarController and top NavigationBar within my app. The tabBar loads in the appDelegate, initializes the navigationBar to use an image instead of center title, and this exists for all four tabBar views. Occasionally I'll push a new view onto the view controllers and that works well enough.

What I'd like to do is be able to change the NavigationBar for each of the 4 tabBar view controllers, when they are opened. I'm having trouble doing this. My initial navigationBar has an image, loaded in the appDelegate. My questions from here are:

  • Should each viewController generally create a new navigationBar from scratch, per its needs, in the respective viewWillAppear method?
  • What navigationController and navigationBar should be editted-- always the appDelegate's navigationController, the tabBarController.navigationController, or simply self.navigationController in each view? (generally, most editing most of these is not working for me, causes no changes to occur)
  • If I override the standard title (which is usually the tabBar's current view's title) with the imageView, and want another tabBar view to use the standard title, how should I remove the titleView image, and reset back to text? And viceVersa, dependent on view? This is what I'm really trying to accomplish unsuccessfully.

I guess I'm looking for standard practice in managing the navigationBar per view, and changing it per tabBarItem.

// in appDelegate
- (void)initNav {
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate = self;
    self.tabBarController.navigationController.view.backgroundColor = [UIColor whiteColor];

    self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstViewController,
                                             self.secondViewController,
                                             self.thirdViewController,
                                             self.forthViewController,
                                             nil];


    [self.window addSubview:self.tabBarController.view];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];
    self.navigationController.navigationBarHidden = NO;

    // customize background color of nav bar to "blue"ish color
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"00A3E1"];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"00A3E1"];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"00A3E1"];

    [self createNavs];
}

// also in appDelegate
- (void)createNavs {
    // white text when present
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor whiteColor],
                                UITextAttributeTextColor,
                                [UIColor clearColor],
                                UITextAttributeTextShadowColor, nil];

    [[UIBarButtonItem appearance] setTitleTextAttributes: attributes
                                                forState: UIControlStateNormal];


    AppDelegate *delegateRef = (AppDelegate*)[[UIApplication sharedApplication] delegate];

    [self.navigationController.navigationBar setTranslucent:NO];

    // setup left button (currently unused -- no left button)
    /*
    /*
    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"glyphicons_049_star.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(starClick:) forControlEvents:UIControlEventTouchDown];
    [button setFrame:CGRectMake(0, 0, 25, 25)];
    self.navigationController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    */

    // setup logo in center
    self.tabBarController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"synced_top_logo.png"]];

    // setup right button (currently unused -- no right button)
    /*
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
    rightButton.title = @"edit";
    [rightButton setTarget:self ];
    [rightButton setAction:@selector(editClick:)];
    self.navigationController.navigationBar.topItem.rightBarButtonItem = rightButton;
    */

    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    [[delegateRef navigationController] setNavigationBarHidden:NO animated:YES];
}

EDIT: This is a simplified version of the solution posted below that was helpful, and allowed the customization that was needed. Each view controller customizations its navigationbar independently from this. It was probably how it should have been done from the start.

tabBarController.viewControllers = [NSArray arrayWithObjects:[[UINavigationController alloc] initWithRootViewController:self.firstViewController], 
                                         [[UINavigationController alloc] initWithRootViewController:self.secondViewController],
                                         [[UINavigationController alloc] initWithRootViewController:self.thirdViewController],
                                         [[UINavigationController alloc] initWithRootViewController:self.forthViewController],
                                         nil];
1
So let me understand this. You have a UITabBarController wrapped in a UINavigationController? So in the first view where the UITabBarController is shown, when you push to a new UIViewController you want the UITabBarController tab bar to disappear? I ask because this is not the convention when using UITabBarController. You should not wrap a UITabBarController in a UINavigationController.random
@Miro, Just a wild guess but have you tried to creadPepe Ramirez
@random, that seems to be what is occurring. I believe I originally did it because the navigationBar was going to be 100% static initially. Now it is reaching its limitation. What's the best approach, a NavigationController for each ViewController added to the TabBarController? They will need unique titles + buttons now, and some will push other views.Miro

1 Answers

2
votes

This maybe what you are needing:

- (void)initTabbar {

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate = self;

    /*
     You want each of your UIViewControllers to be wrapped in a UINavigationController. Then put each of those UINavigationControllers in a UITabBarController
    */

    //You don't need to hang on to this becuase the proceeding UINavigationController will handle it
    FirstViewController *firstViewController = [[FirstViewController alloc] ...];

    //You'll need to declare this in your header
    self.firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];


    //Second view allocation
    SecondViewController *secondViewController = [[SecondViewController alloc] ...];
    self.secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];

    //Third view allocation
    ThirdViewController *thirdViewController = [[ThirdViewController alloc] ...];
    self.thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];


    //Now you add each of the UINavigationControllers (which is a subclass of UIViewController) to the UITabBarController.
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstNavigationController,
                                             self.secondNavigationController,
                                             self.thirdNavigationController,
                                             nil];


    [self.window addSubview:self.tabBarController.view];

    [self createNavs];
}

//This is more of a 'formatNavs' now
- (void)createNavs {

    //Now you can customize each of the UINavigationController's UINavigationBars seperatly
    self.firstNavigationController.navigationBar.backgroundColor    = [UIColor colorWithHexString:@"00A3E1"];
    self.firstNavigationController.navigationBar.tintColor          = [UIColor colorWithHexString:@"00A3E1"];
    self.firstNavigationController.navigationBar.barTintColor       = [UIColor colorWithHexString:@"00A3E1"];

    self.secondNavigationController.navigationBar.backgroundColor   = [UIColor colorWithHexString:@"...."];
    self.secondNavigationController.navigationBar.tintColor         = [UIColor colorWithHexString:@"...."];
    self.secondNavigationController.navigationBar.barTintColor      = [UIColor colorWithHexString:@"...."];

    self.thirdNavigationController.navigationBar.backgroundColor    = [UIColor colorWithHexString:@"...."];
    self.thirdNavigationController.navigationBar.tintColor          = [UIColor colorWithHexString:@"...."];
    self.thirdNavigationController.navigationBar.barTintColor       = [UIColor colorWithHexString:@"...."];
}