1.由于tableView 頭部視圖和尾部視圖出現(xiàn)一塊留白問題
有兩種辦法去掉留白:
1)tableView
的style:UITableViewStyleGrouped
類型凿滤,默認(rèn)tableView
開頭和結(jié)尾是有間距的,不需要這個(gè)間距的話,可以通過實(shí)現(xiàn)heightForHeaderInSection
方法(返回一個(gè)較小值:0.1)和viewForHeaderInSection
(返回一個(gè)view)來去除頭部的留白萧恕,底部同理。
2)iOS 11上發(fā)生tableView
頂部有留白,原因是代碼中只實(shí)現(xiàn)了heightForHeaderInSection
方法怪与,而沒有實(shí)現(xiàn)viewForHeaderInSection
方法。iOS 11之后應(yīng)該是由于開啟了估算行高機(jī)制引起了bug缅疟。添加上viewForHeaderInSection
方法后分别,問題就解決了×裕或者添加以下代碼關(guān)閉估算行高茎杂,問題也得到解決。
可以在使用tableView的地方添上下面的代碼纫雁。
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
在我們的項(xiàng)目中由于項(xiàng)目工程量比較大煌往,所以我們?yōu)榱送祽校oTableView寫了一個(gè)分類:
#import "UITableView+YCEstimatedRowHeight.h"
#import <objc/runtime.h>
@implementation UITableView (YCEstimatedRowHeight)
+ (void)load {
// 編譯時(shí)判斷SDK
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL selectors[] = {
@selector(initWithFrame:style:),
};
for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
SEL originalSelector = selectors[index];
SEL swizzledSelector = NSSelectorFromString([@"yc_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
Method originalMethod = class_getInstanceMethod(self, originalSelector);
Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
BOOL addedSuccess = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (addedSuccess) {
class_replaceMethod(self, swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
});
#endif
}
- (instancetype)yc_initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
if ([self yc_initWithFrame:frame style:style]) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0
self.estimatedRowHeight = 0;
self.estimatedSectionFooterHeight = 0;
self.estimatedSectionHeaderHeight = 0;
#endif
if (iPhoneX) { // 加這句話的目的是防止在iphoneX上,底部的那條黑線擋住內(nèi)容
self.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
}
}
return self;
}
2.修改ios11上automaticallyAdjustsScrollViewInsets失效的問題
ios11給UIScrollView加了一個(gè)新的屬性 contentInsetAdjustmentBehavior 刽脖,可以通過設(shè)置這個(gè)屬性值來解決羞海。下面這種方式比較粗暴,因?yàn)閷?duì)于有的頁面不需要 automaticallyAdjustsScrollViewInsets 設(shè)置這個(gè)屬性曲管,所以選擇性使用却邓。
#import "UIScrollView+YCContentInset.h"
#import <objc/runtime.h>
@implementation UIScrollView (YCContentInset)
+ (void)load {
// 編譯時(shí)判斷SDK
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL selectors[] = {
@selector(initWithFrame:),
};
for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
SEL originalSelector = selectors[index];
SEL swizzledSelector = NSSelectorFromString([@"yc_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
Method originalMethod = class_getInstanceMethod(self, originalSelector);
Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
BOOL addedSuccess = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (addedSuccess) {
class_replaceMethod(self, swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
});
#endif
}
- (instancetype)yc_initWithFrame:(CGRect)frame {
if ([self yc_initWithFrame:frame]) {
if (IOS11) {
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
}
return self;
}