Reputation: 6823
I am trying to work out how to setup a custom navigation bar with storyboards. I have added a navigation bar directly to a view controller in my app.
The VC is not directly in a Navigation Controller stack, but will appear as it is. Kind of as a main point for the application. The same navigation bar will be used through the application.
I would like to style the navigation bar throughout the application. Previously I used the following when it was within a navigation controller, but now isn't and will remain not actually in the nav stack as not required.
[[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault];
What is the correct way to style this now it is an item simply added to the VC? The bar will be added to other VCs too so it should be re-usable. I have tried to subclass UINavigationBar and change this in the storyboard for the Navigation Bar too but not sure how to implement the styling.
I have tried to change the drawRect method on this subclass but the changes do not take place. I cannot find any documentation directly from Apple on how to subclass this and add styling.
Upvotes: 1
Views: 5339
Reputation: 1183
In Xcode 5 (and likely 4), you can change the class of the UINavigationController's UINavigationBar to your own custom class. Then add your customizations as desired in initWithCoder:.
Upvotes: 2
Reputation: 10175
You can try something like:
[[UINavigationBar appearanceWhenContainedIn:[YourViewController1 class],
[YourViewController2 class], nil]
setBackgroundImage:[UIImage imageNamed:@"YourImageName"]
forBarMetrics:UIBarMetricsDefault];
Upvotes: 1
Reputation: 862
Well, I can tell you what worked for me:
Subclass the navigationBar and the UINavigationController too, so in its initWithCoder you assign it with setValue:forKey
- (id)initWithCoder:(NSCoder *)aDecoder {
if(!(self = [super initWithCoder:aDecoder])) return nil;
CDANavigationBar *navBar = [[CDANavigationBar alloc] init];
[self setValue:navBar forKey:@"navigationBar"];
return self;
}
In your StoryBoard you assign the UINavigationController to your own one.
Upvotes: 2