UIViewController扩展hidesNavagationBarWhenPush

#import <UIKit/UIKit.h>

@interface UIViewController (NavigationBar)
@property (nonatomic, assign) IBInspectable BOOL hidesNavigationBarWhenPush;
@end

#import "UIViewController+NavigationBar.h"
#import <objc/runtime.h>

@implementation UIViewController (NavigationBar)

const void *kHidesNavigationBarWhenPush = "kHidesNavigationBarWhenPush";

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        
        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector = @selector(_viewWillAppear:);
        
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class , swizzledSelector);
        
        BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        
        if (didAddMethod) {
            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
        
    });
}

- (void)_viewWillAppear:(BOOL)animated {
    [self _viewWillAppear:animated];
//    self.hidesBottomBarWhenPushed = false;
    [self.navigationController setNavigationBarHidden:self.hidesNavigationBarWhenPush animated:animated];
}

- (BOOL)hidesNavigationBarWhenPush {
    return objc_getAssociatedObject(self, kHidesNavigationBarWhenPush);
}

- (void)setHidesNavigationBarWhenPush:(BOOL)hidesNavigationBarWhenPush {
    objc_setAssociatedObject(self, kHidesNavigationBarWhenPush, [NSNumber numberWithBool:hidesNavigationBarWhenPush], OBJC_ASSOCIATION_ASSIGN);
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容