0

I'd like to do something that I presumed would be quit simple but turned out to be quite hard! I want to style the navigationbar and toolbar of my navigationcontroller. I want them both to have:

  1. A custom background image, I know I can use appearance proxies but this will not meet the requirements of the following point.
  2. The navigationbar and toolbar should be of the same height of the background images that I set for them. I've tried setting the frame height but it didn't work.
  3. Lastly I'd then like to add some buttons (with specific height and widths, not something fixed/standard) to the navigationbar and toolbar, but I hope the toolbar/navigation will just behave like an UIView.

How do I achieve these three points? Thanks!

sougonde
  • 3,438
  • 3
  • 26
  • 35

2 Answers2

1

I'd like to add a better solution (in my opinion): add a category for UINavigationBar and UIToolbar, for example:

UINavigationBar+myNavBar.m

#import "UINavigationBar+myNavBar.h"

@implementation UINavigationBar (myNavBar)
- (CGSize)sizeThatFits:(CGSize)size {
    UIImage *header = [UIImage imageNamed:@"Images/backgrounds/header"];
    CGSize newSize = CGSizeMake(header.size.width,header.size.height);
    return newSize;
}
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"Images/backgrounds/header"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

UINavigationBar+myNavBar.h

#import <UIKit/UIKit.h>

@interface UINavigationBar (myNavBar)
- (CGSize)sizeThatFits:(CGSize)size;
@end

The solution for UIToolbar is almost exactly the same.

sougonde
  • 3,438
  • 3
  • 26
  • 35
0

What I did for this was to create images that looked exactly like the built in navigation bar. I also created a back button which was very close to the default one. You could of course do whatever you like. This did take a fair bit of time but once you've done that, you can just load them into your app as a UIImageView and a UIButton. Easy as pie. And totally customizable.

- (void) viewDidLoad {

    UIView *navBarView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,44)];

    UIImageView *navBarImg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,44)];
    [navBarImg setImage:[UIImage imageNamed:@"navigationBarImage"]]; // the name of the .png file you made
    [navBarView addSubview:navBarImg];

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setBackgroundImage:[UIImage imageNamed:@"backButtonNormal"] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[UIImage imageNamed:@"backButtonHighlighted"] forState:UIControlStateHighlighted];
    [backButton setFrame:CGRectMake(5,5,40,35)]; //Depends on how you do your button
    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [navBarView addSubview:backButton];

    //Repeat for any other buttons you wish to add. 

    [self.view addSubview:navBarView];
}

- (IBAction) backButtonPressed:(id)sender {
   [self.navigationController popViewControllerAnimated:YES];
}


//Pushing VC's
// I usually make a declared property for the VC and then:
- (IBAction) visitNextVC {
   NextVC *_nextVC = [[NextVC alloc] initWithNibName:@"NextVC" bundle:nil];
   [self setNextVC:_nextVC];
   [self.navigationController pushViewController:nextVC animated:YES];
}
bkbeachlabs
  • 2,121
  • 1
  • 22
  • 33
  • Could you please update your answer and add code on how to add ImageViews to the NavigationController view? Since the NavigationController is the "parent" the navigationbar/toolbar don't animate while the other views do (with segues). I want to make sure the navigation/toolbar doesn't animate along! – sougonde Jul 10 '12 at 08:19
  • 1
    Hope that's what you're looking for. Let me know if I've made a mistake or anything. I just typed that in the browser without testing in XCode. – bkbeachlabs Jul 11 '12 at 05:24
  • Thanks a lot! Now the only thing left is: how to push the viewcontrollers from within my navigationcontroller? But I think I'll figure it out. – sougonde Jul 11 '12 at 14:47
  • Thanks, but what is the nibName? I'm using the new storyboarding? – sougonde Jul 11 '12 at 21:00
  • Ahh, I found a clue: http://stackoverflow.com/questions/8848857/how-do-i-change-initwithnibname-in-storyboard – sougonde Jul 11 '12 at 23:18