mkral
mkral

Reputation: 4085

Custom UINavigationController with button

I would like the NavigationBar to behave the same but would like to change the appearance of it. I've found so many ways of doing this online but I'm not sure which one is the best result for iOS 5.0. The navigation bar will look like this:

enter image description here

Upvotes: 0

Views: 347

Answers (2)

Neeku
Neeku

Reputation: 3653

I had been looking for this thing for ages, too, without finding a straightforward solution! Thanks to an friend of mine, and sample codes, we made it with a custom navigation bar class that can be imported into any view controller class.

The .h file:

#import <UIKit/UIKit.h>

@interface NATitleBar : UIView {
    NSInteger tag;
}
@property ( nonatomic) IBOutlet UIImageView *imageView;
@property ( nonatomic) IBOutlet UILabel *label;
@property ( nonatomic) IBOutlet UIButton *back;
@property ( nonatomic) IBOutlet UIButton *home;

/**
 * Supports UIButton-style adding targets
 */

@end

The .m file:

#import "NATitleBar.h"

@implementation NATitleBar
@synthesize imageView;
@synthesize label;
@synthesize back;
@synthesize home;

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

if (self) {
    NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"NATitleBar" owner:self options:nil];
    [self addSubview:[views objectAtIndex:0]];

    // customize the view a bit
    //self.imageView.layer.borderWidth = 1.0;
    //self.imageView.layer.borderColor = [UIColor colorWithWhite:0.4 alpha:0.4].CGColor;
    //self.imageView.clipsToBounds = YES;
    //self.imageView.layer.cornerRadius = 5.0;
}


return self;
}


#pragma mark - Overriden Setters / Getters

- (void)setTag:(NSInteger)aTag {
self.back.tag = aTag;
}

- (NSInteger)tag {
return self.back.tag;
}

@end

and then for the Nib file we have the following:

enter image description here

You can add or delete images in the Nib file to make the GUI as you wish.

Now you must import the class into any view controller you wish to have with custom navigation controller, and also define two methods (or one, if you don't want the 'home' button. in .h :

- (void) back;

in .m:

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

Upvotes: 1

David R&#246;nnqvist
David R&#246;nnqvist

Reputation: 56625

Since you are targeting iOS 5 i would definitely go for customizing UINavigationBar using the Appearance proxy. Then you can easily set your own images and they will apply to all navigation bars in your application without subclassing.

You can also customize the buttons in the navigation bar by customizing UIBarButtonItem. There are method like backButtonBackgroundImageForState:barMetrics: for the back button and backgroundImageForState:barMetrics: for the other buttons.

Upvotes: 3

Related Questions