Ios 在Objective C的所有视图控制器中可重用UIBarButtonItem
我正在使用背景色和字体大小制作自定义导航栏。此外,我在右边有“添加菜单”按钮。为此,我将其分类为 UINavigationController+透明.hIos 在Objective C的所有视图控制器中可重用UIBarButtonItem,ios,objective-c,uinavigationcontroller,uinavigationbar,Ios,Objective C,Uinavigationcontroller,Uinavigationbar,我正在使用背景色和字体大小制作自定义导航栏。此外,我在右边有“添加菜单”按钮。为此,我将其分类为 UINavigationController+透明.h @interface UINavigationController (Transparent) - (void)presentTransparentNavigationBar; - (void)hideTransparentNavigationBar; @end UINavigationController+Transpare
@interface UINavigationController (Transparent)
- (void)presentTransparentNavigationBar;
- (void)hideTransparentNavigationBar;
@end
UINavigationController+Transparent.m
#import "UINavigationController+Transparent.h"
@implementation UINavigationController (Transparent)
UIBarButtonItem *menuButton;
- (void)presentTransparentNavigationBar;
{
menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(showMenu:)];
[menuButton setTintColor:[UIColor whiteColor]];
self.navigationItem.rightBarButtonItem = menuButton;
UIImage *backButtonImage = [UIImage imageNamed:@"back"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self
action:@selector(popViewController)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
[self.navigationBar setTranslucent:NO];
[self.navigationBar setShadowImage:[UIImage new]];
[self.navigationBar setBarTintColor:[UIColor wolfRed]];
[self.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:19.0] }];
[self setNavigationBarHidden:NO animated:NO];
}
- (void)hideTransparentNavigationBar
{
[self setNavigationBarHidden:YES animated:NO];
[self.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
[self.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
@end
在我的视图控制器中,我调用
[self.navigationController presentTransparentNavigationBar];
或
问题是菜单按钮不可见,后退按钮看起来像是标准的iOS蓝色后退按钮。
谢谢你的帮助
请参见我使用以下方法自定义导航栏
目标c
步骤1确保视图控制器嵌入到导航控制器中
步骤2:在资产中添加所需图标,如“菜单图标”和“返回”
步骤3.使用objective-C,您可以根据需要自定义导航栏
我总是这样做
#import "ViewController.h"
@interface ViewController (){UIBarButtonItem *menuButton;}
@end
@implementation ViewController
#pragma mark - life cycle methods
- (void)viewDidLoad {[super viewDidLoad];}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
#pragma mark - IBAction methods
- (IBAction)showClicked:(id)sender {[self presentTransparentNavigationBar];}
- (IBAction)hideClicked:(id)sender {[self hideTransparentNavigationBar];}
#pragma mark - Navigation Bar methods
//Customize Naviagtion Bar
- (void)presentTransparentNavigationBar;
{
//Add Menu Button to Navigaiton Bar
menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(showMenu:)];
[menuButton setTintColor:[UIColor whiteColor]];
self.navigationItem.rightBarButtonItem = menuButton;
//Add Back button to Navigation Bar
UIImage *backButtonImage = [UIImage imageNamed:@"back"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self
action:@selector(popViewController)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
//Config Navigaton bar settings
[self.navigationController.navigationBar setTranslucent:NO];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];//I used light gray color
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:19.0] }];
[self.navigationController.navigationBar setHidden:NO];
}
//Hide Navigation Bar
- (void)hideTransparentNavigationBar
{
[self.navigationController.navigationBar setHidden:YES];
[self.navigationController.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
[self.navigationController.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
- (void) showMenu:(id) sender{ /*TODO when clicks on Menu button */ }
- (void) popViewController{ /*TODO when clicks on Back button */ }
@end
如果一切正常,当你点击屏幕上的“显示”按钮时,你应该能够将菜单和返回按钮转到导航栏
注:
您可以在AppDelegate文件中执行此操作,这样它将在整个应用程序中可用这是一个非常好的答案!但是我如何在每个ViewController上使用相同的菜单按钮而不在每个视图中复制代码呢?谢谢!!您需要在AppDelegate的方法DidFinishLaunchingWithOptions中使用相同的逻辑是否可以初始化UINavigationViewController而不在AppDelegate中定义rootviewController?
#import "ViewController.h"
@interface ViewController (){UIBarButtonItem *menuButton;}
@end
@implementation ViewController
#pragma mark - life cycle methods
- (void)viewDidLoad {[super viewDidLoad];}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
#pragma mark - IBAction methods
- (IBAction)showClicked:(id)sender {[self presentTransparentNavigationBar];}
- (IBAction)hideClicked:(id)sender {[self hideTransparentNavigationBar];}
#pragma mark - Navigation Bar methods
//Customize Naviagtion Bar
- (void)presentTransparentNavigationBar;
{
//Add Menu Button to Navigaiton Bar
menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(showMenu:)];
[menuButton setTintColor:[UIColor whiteColor]];
self.navigationItem.rightBarButtonItem = menuButton;
//Add Back button to Navigation Bar
UIImage *backButtonImage = [UIImage imageNamed:@"back"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self
action:@selector(popViewController)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
//Config Navigaton bar settings
[self.navigationController.navigationBar setTranslucent:NO];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];//I used light gray color
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:19.0] }];
[self.navigationController.navigationBar setHidden:NO];
}
//Hide Navigation Bar
- (void)hideTransparentNavigationBar
{
[self.navigationController.navigationBar setHidden:YES];
[self.navigationController.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
[self.navigationController.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
- (void) showMenu:(id) sender{ /*TODO when clicks on Menu button */ }
- (void) popViewController{ /*TODO when clicks on Back button */ }
@end