I have subclassed UINavigationBar to have a custom navigation bar within my application. The bar is re-used on multiple view controllers with the same style Menu button as the left button item, which is why it is subclassed.
The subclass is then added to the View Controllers navigation bar in the storyboard.
This is the code within the UINavigationBar
subclass:
- (void)drawRect:(CGRect)rect
{
// Drawing code
UIImage *image = [[UIImage imageNamed: @"mainNavBar"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5,0,5)];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
-(void)awakeFromNib {
UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(7.0f, 7.0f, 38.0f, 29.0f)];
[leftButton setImage:[UIImage imageNamed:@"menuBarItem"] forState:UIControlStateNormal];
[leftButton addTarget:nil action:@selector(menuItemPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:leftButton];
}
The issue is Rotation of the Device. I also have some code in appDidFinishLaunching
using the appearance API for additional setup:
// Custom Navigation Bar appearance setup
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5],UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,nil]];
// Used to deal with the rotation of the nav bar when implemented outside of Navigation Controller
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight];
[[UINavigationBar appearance] setContentMode:UIViewContentModeScaleAspectFit];
// Used to push the title down slightly when in Landscape and NavBar outside of Navigation Controller
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:2 forBarMetrics:UIBarMetricsLandscapePhone];
When the device rotates it all works correctly in terms of the bar scaling in height as expected from 44 to 32px. However, the buttons still appear out side of the bar.
I have looked through some other SO posts on this, but cannot figure how to complete this correctly: iPhone: UINavigationBar with buttons - adjust the height
Ideally I do not want to have to deal with the auto rotation in a View Controller because this UINavigationBar is re-used through a large amount of VCs. Adding in auto rotation code would mean each of the VCs would potentially need this too?
EDIT - following answer
If I change the awakeFromNib
to include the following:
UINavigationItem* ni = [[UINavigationItem alloc] initWithTitle:@"Test"];
UIImage *menuBgImage = [UIImage imageNamed:@"menuBarItem"];
UIBarButtonItem *b =[[UIBarButtonItem alloc] initWithImage:menuBgImage landscapeImagePhone:menuBgImage style:UIBarButtonItemStylePlain target:nil action:@selector(menuItemPressed:)];
ni.leftBarButtonItem = b;
self.items = @[ni];
I have the issue of the UIBarButtonItemStylePlain ruining the image:
If I complete the previous setup with initWithCustomView and use the leftButton from the original code. With the leftButton using the autoresize for height- I then get a badly stretched image in landscape?
EDIT 2 - Additional image per answer