Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
like image 693
Josh Kahane Avatar asked Aug 27 '13 19:08

Josh Kahane


2 Answers

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;
}
like image 130
Elyas Naranjee Sani Avatar answered Oct 17 '22 05:10

Elyas Naranjee Sani


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!

like image 45
LuisCien Avatar answered Oct 17 '22 04:10

LuisCien