我为您实现了一个解决方案,它使用一个BarButtonManger
由自定义UINavigationController
子类用作委托的单例。
您仍然必须确定按钮的目标应该是什么(VC 导航到,导航控制器,其他一些单例/应用程序委托,..)但是这个解决方案应该可以帮助您:http
://cl.ly/3M151o1i3z3O
不要忘记将故事板中导航控制器的类更改为 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