UINavigationController在隐藏导航栏时按下弹出窗口

时间:2017-03-29 09:20:54

标签: ios uinavigationcontroller

我有两个ViewControlers(ControllerA,ControllerB),它们都隐藏在viewWillAppear中的导航栏,并在viewWillDisappear中显示navigationBar(因为有时候,控制器将它们推入必须使栏显示)

现在,当我在导航堆栈中安装ControllerA时出现问题,而在ControllerA中,我必须推送ControllerB。

因为推动画会调用A的viewWillDisappear,所以在动画中。该栏将快速显示和隐藏,同样的问题在流行动画中出现。

当viewControllers有不同的显示/隐藏导航设置时,如何很好地进行这种推送和弹出?

1 个答案:

答案 0 :(得分:0)

好的,我得到的解决方案似乎运作良好,我在这里分享 1创建uiviewcontroller的catetory

#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

和实施

#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

在每个想要隐藏bar的类实现中,这样做:(必须注意的是在你使用的init方法中设置needBarHide标志)

@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