OGeek|极客世界-中国程序员成长平台

标题: ios - UINavigationController 隐藏导航栏时弹出 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:22
标题: ios - UINavigationController 隐藏导航栏时弹出

我有两个 ViewControler (ControllerA, ControllerB),它们都在 viewWillAppear 中隐藏导航栏,在 viewWillDisappear 中显示导航栏(因为有时, Controller 将它们插入必须使栏显示)

现在,当我在导航堆栈中获得 ControllerA 时遇到问题,而在 ControllerA 中,我必须推送 ControllerB。

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

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



Best Answer-推荐答案


好的,我有一个解决方案似乎效果很好,我在这里分享 1st创建一个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 objCTypeencode(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)initWithCoderNSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        self.needBarHide = YES;
    }
    return self;
}

- (instancetype) initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nil];
    if (self)
    {
        self.needBarHide = YES;
    }
    return self;
}


- (void)viewWillAppearBOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.delegateObject = self.navigationController.interactivePopGestureRecognizer.delegate;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (void)viewWillDisappearBOOL)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

关于ios - UINavigationController 隐藏导航栏时弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43089400/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4