有没有办法在ViewDidLoad或全局某种程度上实现以下代码的导航栏样式,而不是粘贴在每个VC中?谢谢。
// Navagation styling
viewController.title = @"NAV TITLE";
[viewController.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Gibson-Light" size:20.0], NSFontAttributeName, nil]];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:nil
style:UIBarButtonItemStylePlain
target:viewController
action:nil];
rightBarButton.image = [UIImage imageNamed:@"settings_cog"];
rightBarButton.tintColor = [UIColor grayColor];
viewController.navigationItem.rightBarButtonItem = rightBarButton;
答案 0 :(得分:2)
您可以创建一个BaseViewController并将上面的代码放入其中,并从BaseViewController扩展所有控制器。
这样做:
创建一个Objective-C类BaseViewController.h和.m
在BaseViewController.h中
@interface BaseViewController : UIViewController
{
NSString *_title;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)navTitle;
@end
在BaseViewController.m
@implementation BaseViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)navTitle
{
_title = navTitle;
}
- (void)viewDidLoad
{
self.title = _title;
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Gibson-Light" size:20.0], NSFontAttributeName, nil]];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:nil
style:UIBarButtonItemStylePlain
target:viewController
action:nil];
rightBarButton.image = [UIImage imageNamed:@"settings_cog"];
rightBarButton.tintColor = [UIColor grayColor];
self.navigationItem.rightBarButtonItem = rightBarButton;
}
@end
现在您可以创建视图控制器并从BaseViewController扩展它,如果是UIViewController。
如果您有任何问题,请与我们联系。