iOS 进阶

runtime解决iOS11 tableview上移下移问题

2017-09-14  本文已影响524人  Love_iOS

runtime解决iOS11 tableview上移下移问题

给tableview创建一个分类,然后交换初始化方法

#import "UITableView+Ex.h"

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

-(instancetype)mr_tableView_initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    UITableView *tableview = [self mr_tableView_initWithFrame:frame style:style];
    if (tableview) {
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return tableview;
}
@end

上一篇下一篇

猜你喜欢

热点阅读