统一设置UINavigationController左上角图标

方法一

通过工厂方法,然后在需要左上角显示图标的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--