1. 程式人生 > >OC 簡單的自定義BaseNavigationController與BaseViewController

OC 簡單的自定義BaseNavigationController與BaseViewController

1.建立控制器BaseNavigationController繼承自UINavigationController:

#import "BaseNavigationController.h"

@interface BaseNavigationController ()<UINavigationControllerDelegate>

@end

@implementation BaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.delegate = self;
    //設定navigationBar顏色
    [self.navigationBar setBackgroundImage:[UIImage imageWithColor:App_Main_Color rect:CGRectMake(0, 0, UI_SCREEN_WIDTH, HeightForNagivationBarAndStatusBar)] forBarMetrics:UIBarMetricsDefault];
    //毛玻璃效果
    self.navigationBar.translucent = NO;
    //設定標題的樣式
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
#pragma mark - 控制器檢視將要出現時
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    //通過對當前navigationController中viewControllers的數量來判斷是否需要隱藏tabBar
    if (self.viewControllers.count > 1) {
        self.tabBarController.tabBar.hidden = YES;
    }else {
        self.tabBarController.tabBar.hidden = NO;
    }
}

2.建立BaseViewControllerk繼承自UIViewController:

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //判斷是否有上級頁面來確定是否顯示返回按鈕
    if (self.navigationController.viewControllers.count > 1) {
        [self setLeftBarButton];
    }
}
#pragma mark - 自定義返回按鈕
- (void)setLeftBarButton{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon-fh"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonClick)];
}
#pragma mark - 返回按鈕點選事件
- (void)leftBarButtonClick{
    [self.navigationController popViewControllerAnimated:YES];
}

3.在AppDelegate中對上述兩個基類簡單實用:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    Item1ViewController *v1 = [[Item1ViewController alloc]init];
    BaseNavigationController *n1 = [[BaseNavigationController alloc]initWithRootViewController:v1];
    Item2ViewController *v2 = [[Item2ViewController alloc]init];
    BaseNavigationController *n2 = [[BaseNavigationController alloc]initWithRootViewController:v2];
    
    self.tabBarController = [[BaseTabBarController alloc]init];
    self.tabBarController.viewControllers = @[n1,n2];
    
    UITabBar *tabBar = self.tabBarController.tabBar;
    
    UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
    item1.title = @"item1";
    
    UITabBarItem *item2 = [tabBar.items objectAtIndex:1];
    item2.title = @"item2";
    
    self.window.rootViewController = self.tabBarController;
    
    return YES;
}