Reputation: 607
I think this should be a simple one but I'm left scratching my head. I have an app with multiple ViewControllers setup in a storyboard. I want each of these to have the same navigation bar at the top of every view but this navigation controller to have the same buttons on all views. The navigation controller is setup in AppDelegate and the buttons are added in the ViewDidLoad of each ViewController (as per an answer I received on here for another question) but at the moment, the buttons (and the title) aren't showing.
Where the UINavigationController is setup - AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
MainViewController* mainVC = [mainStoryboard instantiateInitialViewController];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.window setRootViewController:navVC];
[_window makeKeyAndVisible];
return YES;
}
One of the Views:
- (void)viewDidLoad
{
[self setTitle:@"Congress app"]; // < This does not set the title of the NavController
UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showSettings:)];
UIBarButtonItem *showMenuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)];
//self.navigationItem.leftBarButtonItem = showMenuButton;
//self.navigationItem.rightBarButtonItem = showSettingsButton;
self.navigationController.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationController.navigationItem.rightBarButtonItem = showSettingsButton;
UINavigationController *currentNav;
UIViewController *VCname;
NSLog(@"First left button in navigation controller - %@", [self.navigationController.navigationItem.leftBarButtonItems objectAtIndex:0]);
NSLog(@"First right button in navigation controller - %@", [self.navigationController.navigationItem.rightBarButtonItems objectAtIndex:0]);
[super viewDidLoad];
}
I have a couple of NSLogs that show what (if anything) is in the navigation controller, it shows: and which intimates that buttons are being added but not shown?
EDIT:
I have just remembered that there is a ViewController (InitViewController.m) that is fired before the VC in the above code. The NavigationController is allocated here:
- (void)viewDidLoad
{
[super viewDidLoad];
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
}
This is likely to be the cause of the problem.
EDIT 2:
Thanks to Saurav Mac I have tracked the problem down to me trying to add the buttons on the 'wrong' view controller. I had the Navigation Controller setup in AppDelegate.m, the first View Controller is InitViewController which then calls MainViewController to be the 'top view controller'. Can't believe I missed that, thanks for the help.
Upvotes: 1
Views: 14750
Reputation: 50
pls try this ...
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frameimgback1 = CGRectMake(0, 0, 60, 35);
UIButton *button = [[UIButton alloc] initWithFrame:frameimgback1];
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Ravindra" forState:UIControlStateNormal];
UIBarButtonItem *btnL = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = btnL;
}
Upvotes: 0
Reputation: 3089
Try this one:
Display leftBarButtonItem of navigation controller:
UIImage *faceImage = [UIImage imageNamed:@"back_arrow.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
[face addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.leftBarButtonItem = backButton;
[self.navigationItem setHidesBackButton:YES animated:YES];
[self.navigationItem setLeftBarButtonItem:nil animated:NO];
[self.navigationItem setBackBarButtonItem:nil];
Display rightBarButtonItem of navigation controller:
UIImage *faceImage1= [UIImage imageNamed:@"slideshowarrow"];
UIButton *face1 = [UIButton buttonWithType:UIButtonTypeCustom];
face1.bounds = CGRectMake( 10, 0, faceImage1.size.width, faceImage1.size.height );
[face1 addTarget:self action:@selector(Goto_nextView) forControlEvents:UIControlEventTouchUpInside];
[face1 setImage:faceImage1 forState:UIControlStateNormal];
UIBarButtonItem *backButton1 = [[UIBarButtonItem alloc] initWithCustomView:face1];
self.navigationItem.rightBarButtonItem = backButton1;
May be it will help you.
Upvotes: 0
Reputation: 82
Either you can create this button in a common class i.e subclass of NSObject and then can call this method in every class you want to add button or you can add this code to ViewDidLoad method of every class -
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]init];
UIImage *img1=[UIImage imageNamed:@"image.png"];
CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height);
UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
[signOut setBackgroundImage:img1 forState:UIControlStateNormal];
[signOut addTarget:self action:@selector(BackButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[signOut setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
self.navigationItem.leftBarButtonItem=barButton;
Upvotes: 6
Reputation: 398
try This ...
In AppDelegate.h
@property (strong, nonatomic) UINavigationController *nav; //navigationControlller Object
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController* mainVC = [mainStoryboard instantiateInitialViewController];
self.nav = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.window setRootViewController:self.nav];
[_window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
One of the Views:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setTitle:@"Congress app"];
UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showSettings:)];
self.navigationItem.leftBarButtonItem = showSettingsButton;
}
-(void)showSettings:(id)sender
{
NSLog(@"showSettings");
}
Upvotes: 0
Reputation: 4744
self.navigationController.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationController.navigationItem.rightBarButtonItem = showSettingsButton;
Change it to
self.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationItem.rightBarButtonItem = showSettingsButton;
Upvotes: 0