美文网首页
第一天10-自定义导航栏控制器

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

作者: March_Cullen | 来源:发表于2017-03-29 15:16 被阅读0次
// 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

相关文章

网友评论

      本文标题:第一天10-自定义导航栏控制器

      本文链接:https://www.haomeiwen.com/subject/pmoeottx.html