美文网首页iOS Development
UIViewController扩展hidesNavagatio

UIViewController扩展hidesNavagatio

作者: anddygon | 来源:发表于2016-03-18 14:38 被阅读97次
    #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
    

    相关文章

      网友评论

        本文标题:UIViewController扩展hidesNavagatio

        本文链接:https://www.haomeiwen.com/subject/jdthlttx.html