分类 标签 存档 黑客派 订阅 搜索

统一设置 UINavigationController 左上角图标

47 浏览

方法一

通过工厂方法,然后在需要左上角显示图标的 ViewController 的 viewDidLoad 方法调用。代码如下:

==Factory.h 文件 ==

#import <Foundation/Foundation.h>

@interface Factory : NSObject
+ (void)addBackButton:(UIViewController *)viewController;
@end

==Factory.m 文件 ==

#import "Factory.h"

@implementation Factory
+ (void)addBackButton:(UIViewController *)viewController{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"community_highlight"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"community_highlight"] forState:UIControlStateHighlighted];
    CGRect frame = button.frame;
    frame.size = button.currentImage.size;
    button.frame = frame;
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    UIBarButtonItem *speaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    speaceItem.width = -5;
    viewController.navigationItem.leftBarButtonItems = @[speaceItem,buttonItem];
}
@end

方法二

这种方法是通过实现 UINavigationController 的 pushViewController 方法,拦截所有 push 进来的 viewController,然后对其进行设置。代码如下:

==BaseNavigationController.h 文件 ==

#import <UIKit/UIKit.h>

@interface BaseNavigationController : UINavigationController

@end

==BaseNavigationController.m 文件 ==

#import "BaseNavigationController.h"

@interface BaseNavigationController ()

@end

@implementation BaseNavigationController

- (void)addBackButton:(UIViewController *)viewController{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"back_icon_red"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"back_icon_red"] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        button.size = button.currentImage.size;
        UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
        UIBarButtonItem *speaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        speaceItem.width = -5;
        viewController.navigationItem.leftBarButtonItems = @[speaceItem,buttonItem];
    }
}

- (void)back{
    [self popViewControllerAnimated:YES];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    NSLog(@"push");
    [self addBackButton:viewController];
    [super pushViewController:viewController animated:animated];
}

#pragma -mark 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end

注意:

  1. 用方法一如果某个 viewController 需要单独设置 navigationItem 的图标,则在 ViewController 的 viewDidLoad 方法里设置即可。
  2. 用了方法二如果在 push 之前调用设置 view 样式有关的方法,则先调用 viewDidLoad 方法,然后调用 push 方法。这时如果在 viewDidLoad 方法里设置 navigationItem 的图标将跟 push 方法拦截 viewController 设置的效果一样。

所谓的拦截,大部分都是通过自定义类,然后重写自带方法实现的。

--EOF--

评论  
留下你的脚步
推荐阅读