• [BS-10] 统一设置app所有页面的“返回”按钮样式


    统一设置app所有页面的“返回”按钮样式

    如果想统一设置app所有页面的“返回”按钮样式,首先自定义WZNavigationController类继承UINavigationController类,然后在自定义类中重写pushViewController: animated:方法即可。

    //重写navC的pushVC方法,以便统一设置push进来的vc的左侧“返回”按钮
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
        
        viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    
        //这句会立即调用被push的VC的viewDidLoad方法,必须放在最后面,否则上面的代码会对viewDidLoad中代码造成覆盖,导致viewDidLoad设置无效。
        [super pushViewController:viewController animated:animated];//animated换为NO,所有VC没有动画
    }

    统一设置app所有页面的LeftBarButton按钮样式Demo

    //WZNavigationController.h
    
    #import <UIKit/UIKit.h>
    @interface WZNavigationController : UINavigationController
    @end
    
    
    //WZNavigationController.m
    
    #import "WZNavigationController.h"
    
    @implementation WZNavigationController
    
    //第一次使用该类时调用该方法(只调用一次)
    + (void)initialize {
    
        //UINavigationBar *bar = [UINavigationBar appearance];使用这个以后所有其他自定义类的nav都会使用该背景色
        UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:[self class], nil];//只在本类及子类有效
        [bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //设置导航栏的背景图片(放在这每次创建nav都会调用)
        //[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
        
        //设置导航栏染色描边颜色
        //self.navigationBar.tintColor = [UIColor blackColor];
    }
    
    
    
    /**
     *  作法1. 苹果默认只能让你在当前vc修改下一vc左侧Back按钮的名字,不能修改字体颜色和高亮颜色,也不能添加自定义按钮;
     *  通过self.navigationBar.tintColor = [UIColor blackColor];可以修改“返回”颜色(默认蓝色),但该方法却怎么也实现不了长按“返回”高亮时的红色。
     */
    
    /*
    //重写navC的pushVC方法,以便统一设置push进来的vc的左侧“返回”按钮
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
        
        viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    
        //这句会立即调用被push的VC的viewDidLoad方法,必须放在最后面,否则上面的代码会对viewDidLoad中代码造成覆盖,导致viewDidLoad设置无效。
        [super pushViewController:viewController animated:animated];//animated换为NO,所有VC没有动画
    }
    */
    
    
    /**
     *  作法2. 因为苹果默认不让添加自定义按钮到本页面管理的下一页面的Back按钮,那么我们可以自定义下一页面的leftBarButtonItem;
    
     */
    
    //重写navC的pushVC方法,以便统一设置push进来的vc的左侧“返回”按钮
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
        
        if(self.viewControllers.count >= 1)
        {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.bounds = CGRectMake(0, 0, 100, 21); //必须设置尺寸大小
            [btn setTitle:@"返回" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
            [btn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];//此处不需要拉伸,所以setImage,不能错写成BackgroundImage了
            //设置btn内容水平靠左
            btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            //设置-10的内边距,让它更靠左(-10特殊用法)
            btn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
            //btn添加事件
            [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
            viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
            //当vc被push进入nav时隐藏底部bar
            viewController.hidesBottomBarWhenPushed = YES;
        
        }
        
        //这句会立即调用被push的VC的viewDidLoad方法,必须放在最后面,否则上面的代码会对viewDidLoad中代码造成覆盖,导致viewDidLoad设置无效。
        [super pushViewController:viewController animated:animated];//animated换为NO,所有VC没有动画
    }
    
    - (void)back {
        [self popViewControllerAnimated:YES];
    }
    
    @end
    iOS开发者交流群:180080550
  • 相关阅读:
    《你的灯还亮着吗》读后感1
    找"1"
    阅读计划---《梦断代码》3
    阅读计划---《梦断代码》2
    个人工作总结(10)
    个人工作总结(9)
    个人工作总结(8)
    个人工作总结(7)
    学习进度条
    个人工作总结(6)
  • 原文地址:https://www.cnblogs.com/stevenwuzheng/p/5434300.html
Copyright © 2020-2023  润新知