{"id":27887510,"url":"https://github.com/wangrui460/backbtneventintercept","last_synced_at":"2025-05-05T08:06:48.969Z","repository":{"id":264141317,"uuid":"89048529","full_name":"wangrui460/BackBtnEventIntercept","owner":"wangrui460","description":"系统返回按钮事件拦截","archived":false,"fork":false,"pushed_at":"2021-07-09T09:58:57.000Z","size":17,"stargazers_count":7,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-05T08:06:41.476Z","etag":null,"topics":["backbtnhandlersample","backbutton","shouldpop","systemback"],"latest_commit_sha":null,"homepage":null,"language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wangrui460.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-22T06:08:33.000Z","updated_at":"2022-04-08T09:13:51.000Z","dependencies_parsed_at":"2024-11-22T09:15:12.274Z","dependency_job_id":null,"html_url":"https://github.com/wangrui460/BackBtnEventIntercept","commit_stats":null,"previous_names":["wangrui460/backbtneventintercept"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangrui460%2FBackBtnEventIntercept","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangrui460%2FBackBtnEventIntercept/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangrui460%2FBackBtnEventIntercept/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangrui460%2FBackBtnEventIntercept/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wangrui460","download_url":"https://codeload.github.com/wangrui460/BackBtnEventIntercept/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252463424,"owners_count":21751763,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backbtnhandlersample","backbutton","shouldpop","systemback"],"created_at":"2025-05-05T08:06:48.256Z","updated_at":"2025-05-05T08:06:48.964Z","avatar_url":"https://github.com/wangrui460.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"## iOS 技术交流\n我创建了一个 微信 iOS 技术交流群，欢迎小伙伴们加入一起交流学习~\n\t\n可以加我微信我拉你进去（备注iOS），我的微信号 wr1204607318\n\n## BackBtnEventIntercept\n系统返回按钮事件拦截\n[Swift 版本](https://github.com/wangrui460/BackBtnEventIntercept_swift)\n\n- **主要实现原理**\n\n\u003cpre\u003e\u003ccode\u003e\n@implementation UINavigationController (ShouldPopOnBackBtn)\n\n- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item\n{\n    BOOL shouldPop = YES;\n    // 看一下当前控制器有没有实现代理方法 currentViewControllerShouldPop\n    // 如果实现了，根据当前控制器的代理方法的返回值决定\n    // 没过没有实现 shouldPop = YES\n    UIViewController* currentVC = [self topViewController];\n    if([currentVC respondsToSelector:@selector(currentViewControllerShouldPop)]) {\n        shouldPop = [currentVC currentViewControllerShouldPop];\n    }\n\n    if(shouldPop)\n    {\n        dispatch_async(dispatch_get_main_queue(), ^{\n            [self popViewControllerAnimated:YES];\n        });\n        // 这里要return, 否则这个方法将会被再次调用\n        return YES;\n    }\n    else\n    {\n        // 让系统backIndicator 按钮透明度恢复为1\n        for(UIView *subview in [navigationBar subviews]) {\n            if(0. \u003c subview.alpha \u0026\u0026 subview.alpha \u003c 1.) {\n                [UIView animateWithDuration:.25 animations:^{\n                    subview.alpha = 1.;\n                }];\n            }\n        }\n        return NO;\n    }\n}\n\n@end\n\u003c/code\u003e\u003c/pre\u003e\n\n- **如何使用**\n\n\u003cpre\u003e\u003ccode\u003e\n// 第一步：导入分类头文件\n#import \"UIViewController+BackBtnEventIntercept.h\"\n\n// 第二步：实现代理方法，return NO 则拦截了系统的返回按钮事件\n- (BOOL)currentViewControllerShouldPop\n{\n    return NO;\n}\n\u003c/code\u003e\u003c/pre\u003e\n\n- **如何禁用系统👉右滑返回手势**\n\n\u003cpre\u003e\u003ccode\u003e\n- (void)viewDidAppear:(BOOL)animated\n{\n    [super viewDidAppear:animated];\n    // 为当前控制器禁用👉右滑返回手势\n    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {\n        self.navigationController.interactivePopGestureRecognizer.enabled = NO;\n    }\n}\n\n- (void)viewWillDisappear:(BOOL)animated\n{\n    [super viewWillDisappear:animated];\n    // 为其他控制器开启👉右滑返回手势\n    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {\n        self.navigationController.interactivePopGestureRecognizer.enabled = YES;\n    }\n}\n\u003c/code\u003e\u003c/pre\u003e\n\n\n### 联系我\n扫码回复1获取面试资料（持续更新）\n![](https://user-images.githubusercontent.com/11909313/123933944-6a4abe00-d9c5-11eb-83ca-379313a2af7c.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangrui460%2Fbackbtneventintercept","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwangrui460%2Fbackbtneventintercept","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangrui460%2Fbackbtneventintercept/lists"}