OK ,i got a solution seems work well , and i share it here
1st create a catetory of uiviewcontroller
#import <UIKit/UIKit.h>
#pragma mark Catalog Property Synthesize Support
#import <objc/runtime.h>
#define SYNTHESIZE_CATALOG_OBJ_PROPERTY(propertyGetter, propertySetter) \
- (id) propertyGetter { \
return objc_getAssociatedObject(self, @selector( propertyGetter )); \
} \
- (void) propertySetter (id)obj{ \
objc_setAssociatedObject(self, @selector( propertyGetter ), obj, OBJC_ASSOCIATION_RETAIN_NONATOMIC); \
}
#define SYNTHESIZE_CATALOG_VALUE_PROPERTY(valueType, propertyGetter, propertySetter) \
- (valueType) propertyGetter { \
valueType ret = {0}; \
[objc_getAssociatedObject(self, @selector( propertyGetter )) getValue:&ret]; \
return ret; \
} \
- (void) propertySetter (valueType)value{ \
NSValue *valueObj = [NSValue valueWithBytes:&value objCType:@encode(valueType)]; \
objc_setAssociatedObject(self, @selector( propertyGetter ), valueObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC); \
}
@interface UIViewController (NavigationBar)
@property(nonatomic,assign) BOOL needBarHide;
- (BOOL)checkPreviousControllerHideFlagInViewWillDisappear;
@end
and the implementation
#import "UIViewController+NavigationBar.h"
@implementation UIViewController (NavigationBar)
SYNTHESIZE_CATALOG_VALUE_PROPERTY(BOOL, needBarHide, setNeedBarHide:)
- (BOOL)checkPreviousControllerHideFlagInViewWillDisappear
{
BOOL lastVCFlag = [self.navigationController.viewControllers lastObject].needBarHide;
return lastVCFlag;
}
@end
in every class implementation which want to hide bar, do it like this:(the thing must be noticed is that set the needBarHide flag in the init method you use)
@implementation RedViewController
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
self.needBarHide = YES;
}
return self;
}
- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nil];
if (self)
{
self.needBarHide = YES;
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
self.delegateObject = self.navigationController.interactivePopGestureRecognizer.delegate;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
BOOL hide = [self checkPreviousControllerHideFlagInViewWillDisappear];
[self.navigationController setNavigationBarHidden:hide animated:YES];
NSLog(@"red viewWillDisappear hide:%d",hide);
self.navigationController.interactivePopGestureRecognizer.delegate = self.delegateObject;
}
@end