I implemented a solution for you, it uses a singleton BarButtonManger
which is used by a custom UINavigationController
-subclass as delegate.
You still have to decide what the target of the button should be (VC navigated to, navigaiton controller, some other singleton / app delegate,..) but this solution should help you: http://cl.ly/3M151o1i3z3O
Dont forget to change the class of the navigation controllers in your storyboard to CustomNavigationController!
// BarButtonManager.h
#import <UIKit/UIKit.h>
@interface BarButtonManager : NSObject <UINavigationControllerDelegate>
+ (BarButtonManager*)sharedInstance;
@end
// BarButtonManager.m
#import "BarButtonManager.h"
@implementation BarButtonManager {
UIBarButtonItem *_sharedItem;
}
+ (BarButtonManager*)sharedInstance
{
static BarButtonManager *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[BarButtonManager alloc] init];
});
return instance;
}
- (UIBarButtonItem*)sharedButtonItem
{
if (!_sharedItem) { // not thread safe, but let's assume this is only called from UI thread
UIButton *nowPlayingButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *background = [[UIImage imageNamed:@"nav-bar-now-playing-button-silver.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(4.0, 7.5, 5.0, 13.0)];
UIImage *backgroundPressed = [[UIImage imageNamed:@"nav-bar-now-playing-button-pressed-silver.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(4.0, 7.5, 5.0, 13.0)];
[nowPlayingButton setBackgroundImage:background forState:UIControlStateNormal];
[nowPlayingButton setBackgroundImage:backgroundPressed forState:UIControlStateHighlighted];
nowPlayingButton.frame = CGRectMake(0.0, 0.0, background.size.width, background.size.height);
NSMutableParagraphStyle *centre = [[NSMutableParagraphStyle alloc] init];
centre.alignment = NSTextAlignmentCenter;
centre.lineBreakMode = NSLineBreakByWordWrapping;
NSMutableAttributedString *nowPlayingTitle = [[NSMutableAttributedString alloc] initWithString:@"Now Playing"];
[nowPlayingTitle addAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:9.5], NSParagraphStyleAttributeName : centre, NSForegroundColorAttributeName : [UIColor whiteColor]} range:NSMakeRange(0, nowPlayingTitle.length)];
[nowPlayingButton setAttributedTitle:nowPlayingTitle forState:UIControlStateNormal];
nowPlayingButton.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
nowPlayingButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
nowPlayingButton.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
[nowPlayingButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -3, 0, 5)];
[nowPlayingButton addTarget:nil action:@selector(nowPlayingPressed) forControlEvents:UIControlEventTouchUpInside];
_sharedItem = [[UIBarButtonItem alloc] initWithCustomView:nowPlayingButton];
}
return _sharedItem;
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
navigationController.topViewController.navigationItem.rightBarButtonItem = nil;
UIBarButtonItem *sharedItem = [self sharedButtonItem];
sharedItem.target = viewController;
viewController.navigationItem.rightBarButtonItem = sharedItem;
}
@end
// CustomNavigationController.h
#import <UIKit/UIKit.h>
@interface CustomNavigationController : UINavigationController
@end
// CustomNavigationController.m
#import "CustomNavigationController.h"
#import "BarButtonManager.h"
@implementation CustomNavigationController
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.delegate = [BarButtonManager sharedInstance];
}
return self;
}
@end