美文网首页iOS 进阶
runtime解决iOS11 tableview上移下移问题

runtime解决iOS11 tableview上移下移问题

作者: Love_iOS | 来源:发表于2017-09-14 14:55 被阅读524次

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

相关文章

网友评论

    本文标题:runtime解决iOS11 tableview上移下移问题

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