適配iOS11 - UITableview UICollectionView

1.升級(jí)Xcode 9 + iOS 11后,發(fā)現(xiàn)原本沒(méi)問(wèn)題,我的項(xiàng)目是支持ipad的,此時(shí)ios11系統(tǒng)的ipad會(huì)出現(xiàn)兩側(cè)空白。


首先如果你的app只支持ios 9及之上的,解決起來(lái)很簡(jiǎn)單,self.tableView.cellLayoutMarginsFollowReadableWidth = NO; 一句代碼就行.

如果要支持更低版本,那就用自定義Cell好了.想顯示文本用UILabel,想顯示圖片用UIImageView,分割線也可以用自己的,位置當(dāng)然也由自己控制.有些人可能不想自己添加,就想用系統(tǒng)的textLabel,detailTextLabel和imageView.那就需要重寫- (void)layoutSubviews方法.如下

- (void)layoutSubviews

{

[super layoutSubviews];

CGRect rect;

if (self.imageView.image) {

rect = self.imageView.frame;

rect.origin.x = 20;

self.imageView.frame = rect;

rect = self.textLabel.frame;

rect.origin.x = CGRectGetMaxX(self.imageView.frame) + 10;

self.textLabel.frame = rect;

rect = self.detailTextLabel.frame;

rect.origin.x = CGRectGetMaxX(self.imageView.frame) + 10;

self.detailTextLabel.frame = rect;

}else{

rect = self.textLabel.frame;

rect.origin.x = 20;

self.textLabel.frame = rect;

rect = self.detailTextLabel.frame;

rect.origin.x = 20;

self.detailTextLabel.frame = rect;

}

上面代碼意思就是textLabel,detailTextLabel和imageView的大小以及垂直方向位置還是由系統(tǒng)決定,只有水平方向的x值由我們自己控制.然后就可以方便的使用cell了.

另外就是分割線的問(wèn)題,自己定義就沒(méi)什么好講,如果用系統(tǒng)的,就要在控制器中設(shè)置一下.方法網(wǎng)上有,我就列一下.

在- (void)viewDidLoad中添加如下代碼

if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])? {

[self.tableView setLayoutMargins:UIEdgeInsetsZero];

}

然后在cellForRowAtIndexPath或者willDisplayCell代理方法中添加以下代碼

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

[cell setLayoutMargins:UIEdgeInsetsZero];

}

if ([cell respondsToSelector:@selector(setSeparatorInset:)]){

[cell setSeparatorInset:UIEdgeInsetsMake(0, -1000, 0, -1000)];

}

注意的是[cell setSeparatorInset:UIEdgeInsetsMake(0, -1000, 0, -1000)];代碼,一般都是用UIEdgeInsetsZero,但在這里不行,需要設(shè)置負(fù)值,并且要設(shè)置的大一點(diǎn).然后就可以了.

2.升級(jí)Xcode 9 + iOS 11后,發(fā)現(xiàn)原本沒(méi)問(wèn)題的collectionView和tableView像是中了風(fēng)一樣,頭部刷新UI出現(xiàn)了錯(cuò)亂。

查閱發(fā)現(xiàn) iOS11棄用了automaticallyAdjustsScrollViewInsets屬性,新增contentInsetAdjustmentBehavior來(lái)替代它

關(guān)于 contentInsetAdjustmentBehavior

@available(iOS?11.0,?*)

public?enum?UIScrollViewContentInsetAdjustmentBehavior?:?Int?{

case?automatic?//?Similar?to?.scrollableAxes,?but?will?also?adjust?the?top?&?bottom?contentInset?when?the?scroll?view?is?owned?by?a?view?controller?with?automaticallyAdjustsScrollViewContentInset?=?YES?inside?a?navigation?controller,?regardless?of?whether?the?scroll?view?is?scrollable

case?scrollableAxes?//?Edges?for?scrollable?axes?are?adjusted?(i.e.,?contentSize.width/height?>?frame.size.width/height?or?alwaysBounceHorizontal/Vertical?=?YES)

case?never?//?contentInset?is?not?adjusted

case?always?//?contentInset?is?always?adjusted?by?the?scroll?view's?safeAreaInsets

}

UIScrollViewContentInsetAdjustmentBehavior 是一個(gè)枚舉類型,值有以下幾種:

* -automatic 和scrollableAxes一樣,scrollView會(huì)自動(dòng)計(jì)算和適應(yīng)頂部和底部的內(nèi)邊距并且在scrollView 不可滾動(dòng)時(shí),也會(huì)設(shè)置內(nèi)邊距.

* -scrollableAxes 自動(dòng)計(jì)算內(nèi)邊距.

* -never不計(jì)算內(nèi)邊距

* -always 根據(jù)safeAreaInsets 計(jì)算內(nèi)邊距

很顯然,我們這里要設(shè)置為 never

開始適配

OC 中

//聲明tableView的位置?添加下面代碼

if?(@available(iOS?11.0,?*))?{

_tableView.contentInsetAdjustmentBehavior?=?UIScrollViewContentInsetAdjustmentNever;

_tableView.contentInset?=?UIEdgeInsetsMake(64,?0,?49,?0);

_tableView.scrollIndicatorInsets?=?_tableView.contentInset;

}

swift 中

//聲明tableView的位置?添加下面代碼

if?#available(iOS?11.0,?*)?{

tableView.contentInsetAdjustmentBehavior?=?.never

tableView.contentInset?=?UIEdgeInsetsMake(64,?0,?49,?0)

tableView.scrollIndicatorInsets?=?tableView.contentInset

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容