Adding UINavigationBar to UIView Programmatically?

10,932

Solution 1

If you are using the navigation controller from the first view I suggest this in your Appdelegate.m

 UIViewController *viewController = [[MenuViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
        self.window.rootViewController=navController;

but if you have a view which presents your new view that you want have a navbar use this for showing the new view:

UIViewController *viewController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
[self presentViewController:navigationcontroller animated:YES completion:nil];

then you can add your navBarButtons in second view like below:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(btnAddGroupPressed)];
        self.navigationItem.rightBarButtonItem=btnAdd;

    }
    return self;
}

Solution 2

I'm not sure what's going on. Here is the code that I'm using to reproduce your issue:

The *.h file is empty

#import "STTestView.h"

@implementation STTestView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
        navBar.barTintColor = [UIColor purpleColor];
        [self addSubview:navBar];

        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];

        UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"categories"];
        [navItem setRightBarButtonItem:doneItem animated:YES];
        [navBar setItems:[NSArray arrayWithObject:navItem] animated:YES];

        self.backgroundColor = [UIColor darkGrayColor];
    }
    return self;
}

-(void)done:(id)sender {

}

@end

And this is what I'm getting:

enter image description here

Can you please:

  1. Add the code for the whole initWithFrame: method?
  2. Make sure that self.frame and headerHeight are in fact set correctly by the time your code runs?
  3. Remember not to use the backgroundColor property but the barTintColor property

Hope this helps!

Solution 3

I cannot tell you why, but I have experienced this same issue and I traced it to the setBackgroundColor method. To get around it I did:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44.0)];
UINavigationItem *titleItem = [[UINavigationItem alloc] initWithTitle:@"MyNavBar"];
NSDictionary *titleAttributesDictionary =  [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor whiteColor],
                                            UITextAttributeTextColor,
                                            [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0],
                                            UITextAttributeTextShadowColor,
                                            [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
                                            UITextAttributeTextShadowOffset,
                                            [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0],
                                            UITextAttributeFont,
                                            nil];
navBar.titleTextAttributes = titleAttributesDictionary;
navBar.items = @[titleItem];
navBar.tintColor=[UIColor blueColor];
[self addSubview:navBar];

Solution 4

Perhaps, you forgot "navBar.items = @[navItem];" as I dealt with similar issue:

    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, headerHeight)];
    navBar.barTintColor = [UIColor blueColor];
    [self addSubview:navBar];//it is important point, otherwise, you can see only the area without any item

    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];

    [navItem setRightBarButtonItem:doneItem animated:YES];
    [navBar setItems:[NSArray arrayWithObject:navItem] animated:YES];

Use these properties: "barTintColor" to get the color of the navigation bar background, and "tintColor" for the navigation items and bar button items.

Share:
10,932

Related videos on Youtube

Josh Kahane
Author by

Josh Kahane

Updated on October 24, 2022

Comments

  • Josh Kahane
    Josh Kahane over 1 year

    I am trying to add a UINavigationBar to a UIView programmatically and having so success, and can't figure it out. I am trying to add it in my UIView subclass and it simply isn't showing up when I run my app.

    Here is my code:

    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, headerHeight)];
        [navBar setBackgroundColor:[UIColor blueColor]];
        [self addSubview:navBar];
    
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];
    
        UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"categories"];
        [navItem setRightBarButtonItem:doneItem animated:YES];
        [navBar setItems:[NSArray arrayWithObject:navItem] animated:YES];
    
  • Josh Kahane
    Josh Kahane almost 11 years
    Thanks for your suggestion but I have explained in the question. I am trying to add a UINavigationBar to a UIView. No controllers are part of this issue.