1.升級Xcode 9 + iOS 11后,發現原本沒問題,我的項目是支持ipad的,此時ios11系統的ipad會出現兩側空白。
首先如果你的app只支持ios 9及之上的,解決起來很簡單,self.tableView.cellLayoutMarginsFollowReadableWidth = NO; 一句代碼就行.
如果要支持更低版本,那就用自定義Cell好了.想顯示文本用UILabel,想顯示圖片用UIImageView,分割線也可以用自己的,位置當然也由自己控制.有些人可能不想自己添加,就想用系統的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的大小以及垂直方向位置還是由系統決定,只有水平方向的x值由我們自己控制.然后就可以方便的使用cell了.
另外就是分割線的問題,自己定義就沒什么好講,如果用系統的,就要在控制器中設置一下.方法網上有,我就列一下.
在- (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,但在這里不行,需要設置負值,并且要設置的大一點.然后就可以了.
2.升級Xcode 9 + iOS 11后,發現原本沒問題的collectionView和tableView像是中了風一樣,頭部刷新UI出現了錯亂。
查閱發現 iOS11棄用了automaticallyAdjustsScrollViewInsets屬性,新增contentInsetAdjustmentBehavior來替代它
關于 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 是一個枚舉類型,值有以下幾種:
* -automatic 和scrollableAxes一樣,scrollView會自動計算和適應頂部和底部的內邊距并且在scrollView 不可滾動時,也會設置內邊距.
* -scrollableAxes 自動計算內邊距.
* -never不計算內邊距
* -always 根據safeAreaInsets 計算內邊距
很顯然,我們這里要設置為 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
}