1

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.

5
  • 1
    The appearance proxy is how to style the navigation bar. What you are doing should work.
    – memmons
    Commented Apr 30, 2013 at 14:49
  • 1
    use your code in AppDelegate.m in didFinishLaunchingWithOptions method that way you can add any navigation bar to any vc Commented Apr 30, 2013 at 14:51
  • 1
    NOTE: Question is resolved but I cannot answer my own question or delete this.
    – StuartM
    Commented Apr 30, 2013 at 16:22
  • What was the solution?
    – memmons
    Commented Apr 30, 2013 at 18:30
  • I was overriding what I had with a class incorrectly, removing this got it working.
    – StuartM
    Commented May 1, 2013 at 22:45

3 Answers 3

2

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.

4
  • So hacky. Do not do this. There is built-in support for skinning controls via the UIAppearance proxy.
    – memmons
    Commented Apr 30, 2013 at 14:56
  • I guess I'm stuck to the old way. Commented Apr 30, 2013 at 15:02
  • Nothing wrong with doing things the old way, but this is an unsupported approach that is fragile and could break at any time -- Apple makes no guarantees that any of its properties are key-value coding compliant.
    – memmons
    Commented Apr 30, 2013 at 16:09
  • This doesn't really work for me. The rootViewController doesn't respect its UIBarButtonItems this way. I think because it gets init'ed with the coder and then re-instantiated without handling the buttons that have been there before. Correct me if I'm wrong.
    – arnekolja
    Commented Oct 4, 2013 at 13:22
2

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:.

1

You can try something like:

[[UINavigationBar appearanceWhenContainedIn:[YourViewController1 class],
                                            [YourViewController2 class],  nil]  
                         setBackgroundImage:[UIImage imageNamed:@"YourImageName"] 
                              forBarMetrics:UIBarMetricsDefault];
1
  • This is a fairly old question now and we moved on from this. But this would be the correct approach to take here, so accepting answer.
    – StuartM
    Commented May 1, 2014 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.