• iOS-自定义NavigationItem返回按钮【pop返回按钮】


    在用navigationVC时,返回按钮有时候不想用系统的,这里用继承的方式把按钮替换了,同时也可以实现系统的右滑返回,很简单;

    1.创建基类 BasePopViewController

    创建一个用于替换系统返回按钮基类的 BasePopViewController : UIViewController;

    BasePopViewController.m

    #import "BasePopViewController.h"
    
    @interface BasePopViewController ()
    
    @end
    
    @implementation BasePopViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self setNavigationItemBackButton];
        self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    }
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        if (self.navigationController.viewControllers.count > 1) {
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }else{
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        }
    }
    
    /**
     自定义返回按钮
     */
    - (void)setNavigationItemBackButton{
        if (self.navigationController.viewControllers.count > 1) {
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(0, 0, 40, 44);
            btn.adjustsImageWhenHighlighted = NO;
            [btn setImage:[UIImage imageNamed:@"back_off"] forState:UIControlStateNormal];
            ///靠左
            btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
            [btn addTarget:self action:@selector(popBackButtonAction) forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn];
            self.navigationItem.leftBarButtonItems = @[item];
        }
    }
    
    /**
     返回按钮事件
     */
    - (void)popBackButtonAction {
        [self.navigationController popViewControllerAnimated:YES];
    }
    #pragma mark - 下面是设置的状态栏颜色,可忽略
    -(UIStatusBarStyle)preferredStatusBarStyle{
        ///这里设置白色
        return UIStatusBarStyleLightContent;
    }
    -(BOOL)prefersStatusBarHidden{
        return NO;
    }
    @end

    2.引用

    在需要替换系统的返回按钮时,新建VC继承BasePopViewController即可,如果要在新的VC中获取点击的返回按钮事件,在新的VC中重写 popBackButtonAction 事件即可。

  • 相关阅读:
    使用SilverLight构建插件式应用程序(九) —聊天插件客户端的实现
    .NET 访问JAVA的WebService使用SOAP头
    使用SilverLight构建插件式应用程序(七)
    管理类软件的界面模板。
    使用SilverLight构建插件式应用程序(六)
    使用SilverLight开发ARPG游戏(一)
    AS 学习笔记 元件和代码的绑定
    scaleform 学习笔记1
    cocos2dx 获取图片的某像素点的RGBA颜色
    接触的第二个引擎 scaleform
  • 原文地址:https://www.cnblogs.com/wangkejia/p/9322601.html
Copyright © 2020-2023  润新知