• 交换某个类里面的方法


    #import "objc/runtime.h"
    
    void Swizzle(Class c, SEL orig, SEL new) {
        Method origMethod = class_getInstanceMethod(c, orig);
        Method newMethod = class_getInstanceMethod(c, new);
        if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))){
            class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
        } else {
            method_exchangeImplementations(origMethod, newMethod);
        }
    }

    运用场景,运用实例

    int main(int argc, char *argv[])
    {
        @autoreleasepool {
            Swizzle([UINavigationBar class], @selector(drawRect:), @selector(SP_drawRect:));
            Swizzle([UITabBar class], @selector(drawRect:), @selector(SP_drawRect:));
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([SNAppDelegate class]));
        }
    }

    分类里重写些方法

    @interface UINavigationBar (SPExtension)
    
    - (void)SP_drawRect:(CGRect)rect;
    
    @end

    实现

    @implementation UINavigationBar (SPExtension)
    
    + (Class)class {
        return NSClassFromString(@"SPNavigationBar");
    }
    
    - (void)SP_drawRect:(CGRect)rect {
        if (self.tag == SPCommonNavigationBar) {
            if ([self viewWithTag:kSPNaviBarShadowTag] == nil) {
                CGRect rf = CGRectMake(0, 0, 320, 44);
                [[UIImage imageNamed:@"title_bar_background.png"] drawInRect:rf];
                
    //            [[UIImage imageNamed:@""] drawInRect:rf];//sp_navigation_bar_bg
    //            self.backgroundColor = [UIColor clearColor];
                
    //            UIView *shadowLayer = [[UIView alloc] initWithFrame:CGRectMake(0, rect.size.height - 0, rect.size.width, 0)];
    //            shadowLayer.alpha =  1.0;
    //            shadowLayer.tag = kSPNaviBarShadowTag;
    //            
    //            CAGradientLayer *grad2 = [CAGradientLayer layer];
    //            grad2.frame = CGRectMake(0, 0, rect.size.width, 3);
    //            grad2.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithHue:0 saturation:0 brightness:0 alpha:.0] CGColor],  (id)[[UIColor colorWithHue:0 saturation:0 brightness:0 alpha:.5] CGColor], nil];
    //            
    //            [shadowLayer.layer insertSublayer:grad2 atIndex:0];
    //            [self insertSubview:shadowLayer atIndex:10];
    //            [shadowLayer release];
            } 
            else {
                UIView *shadowLayer = [self viewWithTag:kSPNaviBarShadowTag];
                shadowLayer.alpha = 1.0;
                shadowLayer.frame = CGRectMake(0, rect.size.height - 3, rect.size.width, 3);
                CALayer *grad2 = [shadowLayer.layer.sublayers objectAtIndex:0];
                grad2.frame = CGRectMake(0, 0, rect.size.width, 3);
            }
        } else {
            UIView *shadowLayer = [self viewWithTag:kSPNaviBarShadowTag];
            if (shadowLayer) {
                shadowLayer.alpha = 0.0;
            }
            [self SP_drawRect:rect];
        }
    }
    
    @end

    好像就处理了一下背景还是什么的,然而并不怎么高级的感觉

  • 相关阅读:
    drf中 连表深度查询和ListSerializer类包括drf中Response二次封装
    drf中表之间断关联操作
    drf中的序列化家族
    rest_framework框架的封装特点和APIView请求生命周期
    vue项目和django项目交互补充,drf介绍,restful规范
    Vue成绩单
    面向对象编程day2
    面向对象编程day3
    面向对象编程day1
    day13
  • 原文地址:https://www.cnblogs.com/songxing10000/p/4946337.html
Copyright © 2020-2023  润新知