Objective C

时间:2016-04-01 14:24:31

标签: ios objective-c uinavigationcontroller uinavigationbar

我使用背景颜色和字体大小制作自定义navigationBar。另外我在右边添加了菜单按钮。为此我创建了名为

的类别

的UINavigationController + Transparent.h

 @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
在我的ViewControllers中调用

[self.navigationController presentTransparentNavigationBar];

[self.navigationController hideTransparentNavigationBar];

问题是菜单按钮不可见,后退按钮看起来像标准iOS蓝色后退按钮。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

  

请参阅我以前用以下方式自定义导航栏   目标c

步骤1确保您的View Controller嵌入导航控制器

enter image description here

步骤2在资产中添加必需的图标,例如。 " MENU_ICON"和"回来"

使用objective-C的Step.3,您可以根据需要自定义导航栏。

我总是这样做,

#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

如果管理好一切,当您点击屏幕上的“显示”按钮时,您应该能够将“菜单”和“返回”按钮导航到导航栏。

enter image description here

注意:

您可以在AppDelegate文件中执行此操作,以便在整个应用程序中使用