iOS: Positioning navigation bar buttons within custom navigation bar
You'll need to add the leftBarButtonItem and rightBarButtonItem as custom items and mess with the frames.... for example:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,5,buttonImage.size.width,buttonImage.size.height)];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:delegate action:@selector(barButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:titleString forState:UIControlStateNormal];
[button setTitleColor:CUSTOM_BAR_BUTTON_TITLE_COLOR forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont boldSystemFontOfSize:14]];
[[button titleLabel] setShadowColor:CUSTOM_BAR_BUTTON_SHADOW_COLOR];
[[button titleLabel] setShadowOffset:CGSizeMake(0,-1)];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];
[[self navigationItem] setRightBarButtonItem:barButton];
[barButton release];
Erik Frisk
Student of entrepreneurship at Chalmers University of Technology. Fascinated by tech, web culture and the human condition.
Updated on July 21, 2022Comments
-
Erik Frisk almost 2 years
I'm building an app with a custom navigation bar. After some research I decided to do this using a category on UINavigationBar. The navigation bar needs to be a bit larger than usual to accomodate a drop shadow. Here is the code:
#import "UINavigationBar+CustomWithShadow.h" @implementation UINavigationBar (CustomWithShadow) - (void)drawRect:(CGRect)rect { // Change the tint color in order to change color of buttons UIColor *color = [UIColor colorWithHue:0.0 saturation:0.0 brightness:0.0 alpha:0.0]; self.tintColor = color; // Add a custom background image to the navigation bar UIImage *image = [UIImage imageNamed:@"NavBar.png"]; [image drawInRect:CGRectMake(0, 0, self.frame.size.width, 60)]; } - (void)layoutSubviews { self.frame = CGRectMake(0, 20, self.frame.size.width, 60); } @end
The only problem now is that the larger navigation bar means that the navigation bar buttons end up too far down, like so:
Does anyone know how I can correct the position of the buttons?
Thanks for all help!
Update:
I add the buttons to the nav bar in the init method of the view controller like so:
// Create "Add" button for the nav bar UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(createNewEntry:)]; [[self navigationItem] setRightBarButtonItem:addButton]; [addButton release];
-
Erik Frisk almost 13 yearsIs there a simpler way? I've got buttons on subsequent views and would like to avoid adding each of them as a custom item if possible. Feel free to suggest alternatives to the way I added the drop shadow to the navigation bar if you have any :)
-
mostafa tourad almost 13 years@Erik You will have to use a customView-bar-button-item taking the actual button as a subview to fix such offset issues.
-
Erik Frisk almost 13 yearsNo change I'm afraid. The buttons continue to align themselves to the bottom of the navigation bar (which goes down to the end of the drop shadow).
-
Abhishek Yadav almost 13 yearsA simpler way to achieve this on all navigation button would be to create a subclass or category of UINavigationBarButton.
-
Erik Frisk almost 13 yearsThat sounds like a good idea adam! Do you know which methods I would need to override and how, or do you know where I would go to find that information?