IOS自定义系统导航栏:导航栏背景色、状态栏样式以及是否隐藏、拦截系统导航栏的返回点击事件

来源:互联网 发布:jsp导入java类 编辑:程序博客网 时间:2024/05/21 22:23


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  BaseViewController.h  
  3. //  TabBarTest  
  4. //  
  5. //  Created by apple on 15/11/27.  
  6. //  Copyright (c) 2015年 zwhy. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. typedef enum : NSUInteger {  
  12.     NavBarStyleBlack,//黑色背景  
  13.     NavBarStyleWhite//白色背景  
  14. } NavBarStyle;  
  15.   
  16. @interface BaseViewController : UIViewController  
  17.   
  18. @property(nonatomic,assign) BOOL hideStatusBar;  
  19. @property(nonatomic,assign) BOOL hideNavigationBar;  
  20. @property (nonatomic, assign) NavBarStyle navBarStyle;  
  21.   
  22. - (void)hideTabBar;  
  23. - (void)showTabBar;  
  24.   
  25. @end  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.   

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  BaseViewController.m  
  3. //  TabBarTest  
  4. //  
  5. //  Created by apple on 15/11/27.  
  6. //  Copyright (c) 2015年 zwhy. All rights reserved.  
  7. //  
  8.   
  9. #import "BaseViewController.h"  
  10. #import "UIViewController+BackButtonHandler.h"  
  11.   
  12. @interface BaseViewController ()  
  13.   
  14. @end  
  15.   
  16. @implementation BaseViewController  
  17.   
  18. -(void)viewWillAppear:(BOOL)animated  
  19. {  
  20.     [super viewWillAppear:animated];  
  21.     NSLog(@"###%s",__FUNCTION__);  
  22.     [[UIApplication sharedApplication] setStatusBarHidden:self.hideStatusBar==YES?YES:NO];  
  23.     [[UIApplication sharedApplication] setStatusBarStyle:self.navBarStyle==NavBarStyleWhite?UIStatusBarStyleDefault:UIStatusBarStyleLightContent];  
  24.     //隐藏导航栏  
  25.     [self.navigationController setNavigationBarHidden:self.hideNavigationBar==YES?YES:NO];  
  26.     [self initNavBar];  
  27.       
  28. }  
  29.   
  30. - (void)viewWillDisappear:(BOOL)animated  
  31. {  
  32.     [super viewWillDisappear:animated];  
  33.       
  34.     [[UIApplication sharedApplication] setStatusBarHidden:NO];  
  35.     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];  
  36.     [self.navigationController setNavigationBarHidden:NO];  
  37.     //显示导航栏  
  38. }  
  39.   
  40. - (void)viewDidLoad {  
  41.     [super viewDidLoad];  
  42.     self.view.backgroundColor=[UIColor whiteColor];  
  43.     NSLog(@"###%s",__FUNCTION__);  
  44.       
  45. }  
  46.   
  47. #pragma mark - Nav  
  48. -(void)initNavBar  
  49. {  
  50.     //定义导航栏  
  51.     if (self.navBarStyle==NavBarStyleWhite) {  
  52.         NSLog(@"### %s NavBarStyleWhite...",__FUNCTION__);  
  53.         [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];  
  54.         [self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];  
  55.         [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:20.0]}];  
  56.         [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];  
  57.           
  58.     } else {//NavBarStyleBlack  
  59.         NSLog(@"### %s NavBarStyleBlack...",__FUNCTION__);  
  60.         [self.navigationController.navigationBar setBarTintColor:kColorTextGray_51];  
  61.         [self.navigationController.navigationBar setBackgroundColor:kColorTextGray_51];  
  62.         [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20.0]}];  
  63.         [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];  
  64.           
  65.     }  
  66.       
  67.     //自定义返回按钮  
  68.     UIImage *backButtonImage = [[UIImage imageNamed:@"nav_btn_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(04000)];  
  69.     [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];  
  70.       
  71.     //隐藏返回按钮的文字方法一  
  72.     UIBarButtonItem *buttonItem = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil nil];  
  73.     UIOffset offset;  
  74.     offset.horizontal = -500;  
  75.     [buttonItem setBackButtonTitlePositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];  
  76.   
  77.     //隐藏返回按钮的文字方法二  
  78. //    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(turnBack:)];  
  79. //    self.navigationItem.backBarButtonItem = item;  
  80.       
  81. }  
  82.   
  83. -(void)turnBack:(id)sender  
  84. {  
  85.     NSLog(@"%s",__FUNCTION__);  
  86.       
  87. }  
  88.   
  89. #pragma mark - TabBar  
  90. - (void)hideTabBar  
  91. {  
  92.     if (self.tabBarController.tabBar.hidden == YES) {  
  93.         return;  
  94.     }  
  95.     UIView *contentView;  
  96.     if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )  
  97.         contentView = [self.tabBarController.view.subviews objectAtIndex:1];  
  98.     else  
  99.         contentView = [self.tabBarController.view.subviews objectAtIndex:0];  
  100.       
  101.     contentView.frame = CGRectMake(contentView.bounds.origin.x,  contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);  
  102.     self.tabBarController.tabBar.hidden = YES;  
  103.       
  104. }  
  105.   
  106. - (void)showTabBar  
  107. {  
  108.     if (self.tabBarController.tabBar.hidden == NO)  
  109.     {  
  110.         return;  
  111.     }  
  112.     UIView *contentView;  
  113.     if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])  
  114.         contentView = [self.tabBarController.view.subviews objectAtIndex:1];  
  115.     else  
  116.         contentView = [self.tabBarController.view.subviews objectAtIndex:0];  
  117.       
  118.     contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);  
  119.     self.tabBarController.tabBar.hidden = NO;  
  120.       
  121. }  
  122.   
  123. - (void)didReceiveMemoryWarning {  
  124.     [super didReceiveMemoryWarning];  
  125. }  
  126.   
  127. @end  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  UIViewController+BackButtonHandler.h  
  2. //  
  3. //  Created by Sergey Nikitenko on 10/1/13.  
  4. //  Copyright 2013 Sergey Nikitenko. All rights reserved.  
  5. //  
  6. //  Permission is hereby granted, free of charge, to any person obtaining a copy  
  7. //  of this software and associated documentation files (the "Software"), to deal  
  8. //  in the Software without restriction, including without limitation the rights  
  9. //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
  10. //  copies of the Software, and to permit persons to whom the Software is  
  11. //  furnished to do so, subject to the following conditions:  
  12. //  
  13. //  The above copyright notice and this permission notice shall be included in  
  14. //  all copies or substantial portions of the Software.  
  15. //  
  16. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
  17. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
  18. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
  19. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
  20. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
  21. //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
  22. //  THE SOFTWARE.  
  23. //  
  24.   
  25. #import <UIKit/UIKit.h>  
  26.   
  27. @protocol BackButtonHandlerProtocol <NSObject>  
  28. @optional  
  29. // Override this method in UIViewController derived class to handle 'Back' button click  
  30. -(BOOL)navigationShouldPopOnBackButton;  
  31. @end  
  32.   
  33. @interface UIViewController (BackButtonHandler) <BackButtonHandlerProtocol>  
  34.   
  35. @end  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  UIViewController+BackButtonHandler.m  
  3. //  
  4. //  Created by Sergey Nikitenko on 10/1/13.  
  5. //  Copyright 2013 Sergey Nikitenko. All rights reserved.  
  6. //  
  7. //  Permission is hereby granted, free of charge, to any person obtaining a copy  
  8. //  of this software and associated documentation files (the "Software"), to deal  
  9. //  in the Software without restriction, including without limitation the rights  
  10. //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
  11. //  copies of the Software, and to permit persons to whom the Software is  
  12. //  furnished to do so, subject to the following conditions:  
  13. //  
  14. //  The above copyright notice and this permission notice shall be included in  
  15. //  all copies or substantial portions of the Software.  
  16. //  
  17. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
  18. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
  19. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
  20. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
  21. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
  22. //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
  23. //  THE SOFTWARE.  
  24. //  
  25.   
  26. #import "UIViewController+BackButtonHandler.h"  
  27.   
  28. @implementation UIViewController (BackButtonHandler)  
  29.   
  30. @end  
  31.   
  32. @implementation UINavigationController (ShouldPopOnBackButton)  
  33.   
  34. - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {  
  35.       
  36.     if([self.viewControllers count] < [navigationBar.items count]) {  
  37.         return YES;  
  38.     }  
  39.       
  40.     BOOL shouldPop = YES;  
  41.     UIViewController* vc = [self topViewController];  
  42.     if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {  
  43.         shouldPop = [vc navigationShouldPopOnBackButton];  
  44.     }  
  45.       
  46.     if(shouldPop) {  
  47.         dispatch_async(dispatch_get_main_queue(), ^{  
  48.             [self popViewControllerAnimated:YES];  
  49.         });  
  50.     } else {  
  51.         // Workaround for iOS7.1. Thanks to @boliva - http://stackoverflow.com/posts/comments/34452906  
  52.         for(UIView *subview in [navigationBar subviews]) {  
  53.             if(subview.alpha < 1.) {  
  54.                 [UIView animateWithDuration:.25 animations:^{  
  55.                     subview.alpha = 1.;  
  56.                 }];  
  57.             }  
  58.         }  
  59.     }  
  60.       
  61.     return NO;  
  62. }  
  63.   
  64. @end  



然后在需要用到-(BOOL)navigationShouldPopOnBackButton的控制器里面包含UIViewController+BackButtonHandler.h这个头文件,覆盖-(BOOL)navigationShouldPopOnBackButton方法。
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #pragma mark - BackButtonHandlerProtocol  
  2. -(BOOL)navigationShouldPopOnBackButton  
  3. {  
  4.     NSLog(@"%s AAAAA",__FUNCTION__);  
  5.     if ([self.delegate respondsToSelector:@selector(workExperienceDidChange:)]) {  
  6.         [self.delegate workExperienceDidChange:self.workList];  
  7.     }  
  8.     return YES;  
  9. }  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  BaseViewController.h  
  3. //  TabBarTest  
  4. //  
  5. //  Created by apple on 15/11/27.  
  6. //  Copyright (c) 2015年 zwhy. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. typedef enum : NSUInteger {  
  12.     NavBarStyleBlack,//黑色背景  
  13.     NavBarStyleWhite//白色背景  
  14. } NavBarStyle;  
  15.   
  16. @interface BaseViewController : UIViewController  
  17.   
  18. @property(nonatomic,assign) BOOL hideStatusBar;  
  19. @property(nonatomic,assign) BOOL hideNavigationBar;  
  20. @property (nonatomic, assign) NavBarStyle navBarStyle;  
  21.   
  22. - (void)hideTabBar;  
  23. - (void)showTabBar;  
  24.   
  25. @end  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.   

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  BaseViewController.m  
  3. //  TabBarTest  
  4. //  
  5. //  Created by apple on 15/11/27.  
  6. //  Copyright (c) 2015年 zwhy. All rights reserved.  
  7. //  
  8.   
  9. #import "BaseViewController.h"  
  10. #import "UIViewController+BackButtonHandler.h"  
  11.   
  12. @interface BaseViewController ()  
  13.   
  14. @end  
  15.   
  16. @implementation BaseViewController  
  17.   
  18. -(void)viewWillAppear:(BOOL)animated  
  19. {  
  20.     [super viewWillAppear:animated];  
  21.     NSLog(@"###%s",__FUNCTION__);  
  22.     [[UIApplication sharedApplication] setStatusBarHidden:self.hideStatusBar==YES?YES:NO];  
  23.     [[UIApplication sharedApplication] setStatusBarStyle:self.navBarStyle==NavBarStyleWhite?UIStatusBarStyleDefault:UIStatusBarStyleLightContent];  
  24.     //隐藏导航栏  
  25.     [self.navigationController setNavigationBarHidden:self.hideNavigationBar==YES?YES:NO];  
  26.     [self initNavBar];  
  27.       
  28. }  
  29.   
  30. - (void)viewWillDisappear:(BOOL)animated  
  31. {  
  32.     [super viewWillDisappear:animated];  
  33.       
  34.     [[UIApplication sharedApplication] setStatusBarHidden:NO];  
  35.     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];  
  36.     [self.navigationController setNavigationBarHidden:NO];  
  37.     //显示导航栏  
  38. }  
  39.   
  40. - (void)viewDidLoad {  
  41.     [super viewDidLoad];  
  42.     self.view.backgroundColor=[UIColor whiteColor];  
  43.     NSLog(@"###%s",__FUNCTION__);  
  44.       
  45. }  
  46.   
  47. #pragma mark - Nav  
  48. -(void)initNavBar  
  49. {  
  50.     //定义导航栏  
  51.     if (self.navBarStyle==NavBarStyleWhite) {  
  52.         NSLog(@"### %s NavBarStyleWhite...",__FUNCTION__);  
  53.         [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];  
  54.         [self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];  
  55.         [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:20.0]}];  
  56.         [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];  
  57.           
  58.     } else {//NavBarStyleBlack  
  59.         NSLog(@"### %s NavBarStyleBlack...",__FUNCTION__);  
  60.         [self.navigationController.navigationBar setBarTintColor:kColorTextGray_51];  
  61.         [self.navigationController.navigationBar setBackgroundColor:kColorTextGray_51];  
  62.         [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20.0]}];  
  63.         [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];  
  64.           
  65.     }  
  66.       
  67.     //自定义返回按钮  
  68.     UIImage *backButtonImage = [[UIImage imageNamed:@"nav_btn_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(04000)];  
  69.     [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];  
  70.       
  71.     //隐藏返回按钮的文字方法一  
  72.     UIBarButtonItem *buttonItem = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil nil];  
  73.     UIOffset offset;  
  74.     offset.horizontal = -500;  
  75.     [buttonItem setBackButtonTitlePositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];  
  76.   
  77.     //隐藏返回按钮的文字方法二  
  78. //    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(turnBack:)];  
  79. //    self.navigationItem.backBarButtonItem = item;  
  80.       
  81. }  
  82.   
  83. -(void)turnBack:(id)sender  
  84. {  
  85.     NSLog(@"%s",__FUNCTION__);  
  86.       
  87. }  
  88.   
  89. #pragma mark - TabBar  
  90. - (void)hideTabBar  
  91. {  
  92.     if (self.tabBarController.tabBar.hidden == YES) {  
  93.         return;  
  94.     }  
  95.     UIView *contentView;  
  96.     if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )  
  97.         contentView = [self.tabBarController.view.subviews objectAtIndex:1];  
  98.     else  
  99.         contentView = [self.tabBarController.view.subviews objectAtIndex:0];  
  100.       
  101.     contentView.frame = CGRectMake(contentView.bounds.origin.x,  contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);  
  102.     self.tabBarController.tabBar.hidden = YES;  
  103.       
  104. }  
  105.   
  106. - (void)showTabBar  
  107. {  
  108.     if (self.tabBarController.tabBar.hidden == NO)  
  109.     {  
  110.         return;  
  111.     }  
  112.     UIView *contentView;  
  113.     if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])  
  114.         contentView = [self.tabBarController.view.subviews objectAtIndex:1];  
  115.     else  
  116.         contentView = [self.tabBarController.view.subviews objectAtIndex:0];  
  117.       
  118.     contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);  
  119.     self.tabBarController.tabBar.hidden = NO;  
  120.       
  121. }  
  122.   
  123. - (void)didReceiveMemoryWarning {  
  124.     [super didReceiveMemoryWarning];  
  125. }  
  126.   
  127. @end  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  UIViewController+BackButtonHandler.h  
  2. //  
  3. //  Created by Sergey Nikitenko on 10/1/13.  
  4. //  Copyright 2013 Sergey Nikitenko. All rights reserved.  
  5. //  
  6. //  Permission is hereby granted, free of charge, to any person obtaining a copy  
  7. //  of this software and associated documentation files (the "Software"), to deal  
  8. //  in the Software without restriction, including without limitation the rights  
  9. //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
  10. //  copies of the Software, and to permit persons to whom the Software is  
  11. //  furnished to do so, subject to the following conditions:  
  12. //  
  13. //  The above copyright notice and this permission notice shall be included in  
  14. //  all copies or substantial portions of the Software.  
  15. //  
  16. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
  17. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
  18. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
  19. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
  20. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
  21. //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
  22. //  THE SOFTWARE.  
  23. //  
  24.   
  25. #import <UIKit/UIKit.h>  
  26.   
  27. @protocol BackButtonHandlerProtocol <NSObject>  
  28. @optional  
  29. // Override this method in UIViewController derived class to handle 'Back' button click  
  30. -(BOOL)navigationShouldPopOnBackButton;  
  31. @end  
  32.   
  33. @interface UIViewController (BackButtonHandler) <BackButtonHandlerProtocol>  
  34.   
  35. @end  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  UIViewController+BackButtonHandler.m  
  3. //  
  4. //  Created by Sergey Nikitenko on 10/1/13.  
  5. //  Copyright 2013 Sergey Nikitenko. All rights reserved.  
  6. //  
  7. //  Permission is hereby granted, free of charge, to any person obtaining a copy  
  8. //  of this software and associated documentation files (the "Software"), to deal  
  9. //  in the Software without restriction, including without limitation the rights  
  10. //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
  11. //  copies of the Software, and to permit persons to whom the Software is  
  12. //  furnished to do so, subject to the following conditions:  
  13. //  
  14. //  The above copyright notice and this permission notice shall be included in  
  15. //  all copies or substantial portions of the Software.  
  16. //  
  17. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
  18. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
  19. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
  20. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
  21. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
  22. //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
  23. //  THE SOFTWARE.  
  24. //  
  25.   
  26. #import "UIViewController+BackButtonHandler.h"  
  27.   
  28. @implementation UIViewController (BackButtonHandler)  
  29.   
  30. @end  
  31.   
  32. @implementation UINavigationController (ShouldPopOnBackButton)  
  33.   
  34. - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {  
  35.       
  36.     if([self.viewControllers count] < [navigationBar.items count]) {  
  37.         return YES;  
  38.     }  
  39.       
  40.     BOOL shouldPop = YES;  
  41.     UIViewController* vc = [self topViewController];  
  42.     if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {  
  43.         shouldPop = [vc navigationShouldPopOnBackButton];  
  44.     }  
  45.       
  46.     if(shouldPop) {  
  47.         dispatch_async(dispatch_get_main_queue(), ^{  
  48.             [self popViewControllerAnimated:YES];  
  49.         });  
  50.     } else {  
  51.         // Workaround for iOS7.1. Thanks to @boliva - http://stackoverflow.com/posts/comments/34452906  
  52.         for(UIView *subview in [navigationBar subviews]) {  
  53.             if(subview.alpha < 1.) {  
  54.                 [UIView animateWithDuration:.25 animations:^{  
  55.                     subview.alpha = 1.;  
  56.                 }];  
  57.             }  
  58.         }  
  59.     }  
  60.       
  61.     return NO;  
  62. }  
  63.   
  64. @end  



然后在需要用到-(BOOL)navigationShouldPopOnBackButton的控制器里面包含UIViewController+BackButtonHandler.h这个头文件,覆盖-(BOOL)navigationShouldPopOnBackButton方法。
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #pragma mark - BackButtonHandlerProtocol  
  2. -(BOOL)navigationShouldPopOnBackButton  
  3. {  
  4.     NSLog(@"%s AAAAA",__FUNCTION__);  
  5.     if ([self.delegate respondsToSelector:@selector(workExperienceDidChange:)]) {  
  6.         [self.delegate workExperienceDidChange:self.workList];  
  7.     }  
  8.     return YES;  
  9. }  
0 0
原创粉丝点击