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

现在,当我在导航堆栈中获取ControllerA时遇到问题,在ControllerA中,我必须按下ControllerB。

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

当viewControllers具有不同的显示/隐藏导航设置时,如何很好地执行这种push&pop?

最佳答案

好吧,我有一个解决方案似乎很好用,我在这里分享
1创建一个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

和实施
#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

在每个要隐藏栏的类实现中,都应这样操作:(必须注意的是,在您使用的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

10-08 05:35
查看更多