Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, April 16, 2016

have a universal bar button item in navigationController

have a universal bar button item in navigationController


I Have UINavigationController in my app, I want to have a right UIBarButtonItem to be shown in all navigation bar that appear in my application. this button will load menu, so I don't want to add this button in every navigation bar manually, also as the function is loading menu, I don't want to copy/past action for this button.

is there any way to handle this in ViewController.h and .m ?

so the button act as a universal bar button item?

Answer by ahwulf for have a universal bar button item in navigationController


Another option is to make your own navigation bar view as a part of the UIViewController. You can turn off Apple's and build your own. We did this to provide our own controls easier. The only thing you lose is the easy translucency under iOS 7. A lot of apps do this for the same reason we did.

Answer by Stonz2 for have a universal bar button item in navigationController


You can create a new class that inherits from UIViewController and in the viewDidLoad method, create a UIBarButtonItem and add it to the navigationItem as a left/right bar item. Let's say this class is called CustomBarViewController:

UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"hi" style:UIBarButtonItemStylePlain target:self action:@selector(doSomething:)];  [self.navigationItem setRightBarButtonItem:barItem];  

In your existing view controllers, instead of having them inherit from UIViewController in the .h file, you can have them use CustomBarViewController instead:

@interface MyExistingViewController : CustomBarViewController  

You can then put actions into the doSomething: method, or have it pass notifications to your existing view controllers.

Answer by datinc for have a universal bar button item in navigationController


What you can do is subclass the navigation controller. Here is an example

@interface NavigationController : UINavigationController  @end    @interface NavigationController ()   @end    @implementation NavigationController    - (void)viewDidLoad  {      [super viewDidLoad];      for (UIViewController* viewController in self.viewControllers){          // You need to do this because the push is not called if you created this controller as part of the storyboard          [self addButton:viewController.navigationItem];               }  }    -(void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated{      [self addButton:viewController.navigationItem];      [super pushViewController:viewController animated:animated];  }    -(void) addButton:(UINavigationItem *)item{      if (item.rightBarButtonItem == nil){          item.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(action:)];      }  }    -(void) action:(UIBarButtonItem*) button{    }    @end  

Answer by Srikanth for have a universal bar button item in navigationController


This cannot be done unless you use a custom view to look like NavigationBar. By default, NavigationController clears all bar button items when a ViewController is pushed or popped. So for every ViewController, you need to create UIBarButtonItem every time in function

- (void)viewWillAppear:(BOOL)animated  

or use can subclass UINavigationController and do as @rp90 answer.

Answer by George Asda for have a universal bar button item in navigationController


You can add the button into a ContainerView. Thus it will be on at all times. Food for thought....


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.