第一天10-自定义导航栏控制器

// LYNavigationController.h
// Created by YoungLee on **/**/**
// Copyright  (c) **** YoungLee.All rights reserved.

#import <UIKit/UIKit.h>

@interface LYNavigationController : UINavigationController

@end
// LYNavigationController.m
// Created by YoungLee on **/**/**
// copyright (c) **** YoungLee.All rights reserved.

#import "LYNavigationController.h"

@interface LYNavigationController ()

@end

@implementation LYNavigationController

+ (void)initialize {
      // usually someClass is a viewController, e.g. taptap
      UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn;[someClass class], nil];
      [bar setBackgroundImage:[UIImage imageNamed;@"navigationBarBackgroundWhite"]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationBarBackgroundImageWhite"] forBarMetrics:UIBarMetricsDefault];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // set backBarButtonItem's title and stype
    // viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    // set backBarButtonItem's customView and stype

      if (self.childViewController.count > 0) { // if it is not the first viewController which was pushed

      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

      [button setTitle:@"返回" forState:UIControlStateNormal];
      [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      [button setTitleColor:[UIColor redColor] forState:UIControlStateHighLight];
      [button setImage:[UIImage imageNamed:@"backBarButtonImageName"] forState:UIControlStateNormal];
      [button setImage:[UIImage imageNamed:@"backBarButtonHighLightedImageName"] forState:UIControlStateHighLighted];

      // button.size = CGSizeMake(70, 30);
      // button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

      [button sizeToFit];
      button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
      [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

      viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

      // hide BottomBar  when pushed
      viewController.hidesBottomBarWhenPushed = YES;
    }

    // it must be the last code
    [super pushViewController:viewController animated:animated];
}

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

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容