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];
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;
}
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:
Can you please:
initWithFrame:
method?backgroundColor
property but the barTintColor
propertyHope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With