tableView在iOS11默认使用Self-Sizing
tableView的estimatedRowHeight、estimatedSectionHeaderHeight、 estimatedSectionFooterHeight三个高度估算属性由默认的0变成了UITableViewAutomaticDimension,导致很多地方的TableView高度出现了问题。
解决办法简单粗暴,就是实现对应方法或把这三个属性设为0。
但由于项目中涉及的页面很多,一个一个改起来很繁琐,于是使用了runtime机制替换了initWithFrame和initWithFrame:style:方法,默认将Self-Sizing关闭了
#import <UIKit/UIKit.h>
@interface UITableView (ClosedSelfSizing)
@end
#import "UITableView+ClosedSelfSizing.h"
#import <objc/runtime.h>
@implementation UITableView (ClosedSelfSizing)
+(void)load{
/** 获取原始setBackgroundColor方法 */
Method originalM = class_getInstanceMethod([self class], @selector(initWithFrame:style:));
/** 获取自定义的pb_setBackgroundColor方法 */
Method exchangeM = class_getInstanceMethod([self class], @selector(initWithNewFrame:style:));
/** 交换方法 */
method_exchangeImplementations(originalM, exchangeM);
/** 获取原始setBackgroundColor方法 */
Method originalInt = class_getInstanceMethod([self class], @selector(initWithFrame:));
/** 获取自定义的pb_setBackgroundColor方法 */
Method exchangeInt = class_getInstanceMethod([self class], @selector(initWithNewFrame:));
method_exchangeImplementations(originalInt, exchangeInt);
}
/** 自定义的方法 */
- (UITableView *)initWithNewFrame:(CGRect)frame style:(UITableViewStyle)style
{
UITableView * temp = [self initWithNewFrame:frame style:style];
temp.estimatedRowHeight = 0;
temp.estimatedSectionHeaderHeight = 0;
temp.estimatedSectionFooterHeight = 0;
return temp;
}
-(UITableView *)initWithNewFrame:(CGRect)frame
{
UITableView * temp = [self initWithNewFrame:frame];
temp.estimatedRowHeight = 0;
temp.estimatedSectionHeaderHeight = 0;
temp.estimatedSectionFooterHeight = 0;
return temp;
}
@end