2
votes

I Have two ViewControlers (ControllerA , ControllerB) ,both of them are hiding navigation bar in viewWillAppear , and showing navigationBar in viewWillDisappear( because sometimes ,the controller push them in has to make the bar be shown)

Now , i have a problem when i got ControllerA in navigation stack , and in ControllerA , i have to push ControllerB .

because the push animation will call A's viewWillDisappear , so in the animation . the bar will be show and hide quickly , same problem happed in pop animation .

how to do this kind of push&pop nicely when viewControllers got different show/hide navigation setting?

1
In which controller you want to show NavigationBar and hide navigationBar?Abilash Balasubramanian
both A B will hide bar , and A will push B in navigationximmyxiao

1 Answers

0
votes

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