Josh Kahane
Josh Kahane

Reputation: 17159

Adding UINavigationBar to UIView Programmatically?

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];

Upvotes: 11

Views: 10977

Answers (5)

Parv
Parv

Reputation: 31

If you are using xib instead of storyboard, then write these lines in AppDelegate:

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

If you are using storyboard then its good to add NavigationController from Menu.

Upvotes: 0

LuisCien
LuisCien

Reputation: 6432

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!

Upvotes: 5

Darius Miliauskas
Darius Miliauskas

Reputation: 3504

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.

Upvotes: 1

Mike Z
Mike Z

Reputation: 4111

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];

Upvotes: 3

Elyas Naranjee Sani
Elyas Naranjee Sani

Reputation: 487

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;
}

Upvotes: 7

Related Questions