Full Source code: https://github.com/boctor/idev-recipes/tree/master/WoodUINavigation
Problem:
Recreate the wood themed navigation bar of Apple’s iBooks app.
Solution:
First we’ll extract the images from the iBooks app and find the main navigation bar image:
Next we’ll add this wood background image as a subview of the UINavigationBar. We add it at the bottom of the z-order so that the buttons are drawn on top:
UIImageView* imageView = [[[UIImageView alloc] initWithFrame:navigationController.navigationBar.frame] autorelease]; imageView.contentMode = UIViewContentModeLeft; imageView.image = [UIImage imageNamed:@"NavBar-iPhone.png"]; [navigationController.navigationBar insertSubview:imageView atIndex:0];
Then we replace those standard UIBarButtonItems with custom ones. To do this we create custom buttons with stretchable images we find in the iBooks app:
and add the custom buttons as the customView of the UIBarButtonItems:
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:[self woodButtonWithText:@"Store" stretch:CapLeftAndRight]] autorelease]; self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:[self woodButtonWithText:@"Edit" stretch:CapLeftAndRight]] autorelease];
Finally, we add a custom segmented control as the titleView:
segmentControlTitles = [[NSArray arrayWithObjects:@"Books", @"PDFs", nil] retain]; UIImage* dividerImage = [UIImage imageNamed:@"view-control-divider.png"]; self.navigationItem.titleView = [[[CustomSegmentedControl alloc] initWithSegmentCount:segmentControlTitles.count segmentsize:CGSizeMake(BUTTON_SEGMENT_WIDTH, dividerImage.size.height) dividerImage:dividerImage tag:0 delegate:self] autorelease];
Our end result is nearly indistinguishable from the original:
Full Source code: https://github.com/boctor/idev-recipes/tree/master/WoodUINavigation
I’ve been wondering how to do this. Thanks for the tip!
Thanks for the tip!
Is this possible to apply the same effect to the “arrow-shape back button” of the navigation bar ? It seems like the instagram app does this.
Hi Mathieu, Instagram uses a fixed-width image for the back button called ‘navigationBarBackButton.png’. My guess is that they create a custom button with this image as the background and then set the button’s text to ‘Back’.
Hi Peter, thanks for the reply. Having a fixed-width image is not really convenient if you need to a longer text.
Maybe it’s better to have an image with the arrow for the left cap and just a straight line for the right cap and then stretch the stretchable area to fit the text length. I’m going to try that. Thanks !
After last update, Instagram use a short navigationBarBackButton.png probably stretched in left-top cap to create a long button.
Is there a way to add these woodsy buttons into the toolbar or navbar in IB?
Good job, thank you very much.
This approach no longer works in iOS 5.0, but using a new method to set a background image does work:
[nc.navigationBar setBackgroundImage:[UIImage imageNamed:@”NavBar-iPhone.png”] forBarMetrics:UIBarMetricsDefault];
Nice! I was just trying to figure it out! Thank you 🙂
Hi all, I feel like a doofus. Not having worked with Ruby before, I’m stuck at step 1. When I run app crush, I just get a return:
Davids-MacPro:~ david$ /Users/david/Desktop/boctor-idev-recipes-40cd85d/Utilities/appcrush/appcrush.rb ‘/Users/david/Desktop/boctor-idev-recipes-40cd85d/Utilities/appcrush/NoteToSelf.ipa ‘
Davids-MacPro:~ david$
Help!
Awesome effect — thanks for explaining 🙂
is it possible to create back button ibook…?