適配 iOS 11 & iPhone X

ios11適配中的問題及解決辦法


1. 滾動條高度跳動、上下拉刷新問題:
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
2. 列表/頁面偏移

本來是這樣的

if (@available(iOS 11.0, *)){
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }

目前發(fā)現(xiàn)所有的Scrollview 及其子類都需要設(shè)置 contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever ,工程中大量使用列表的同學不要慌,不要忙,因為UIView及其子類都遵循UIAppearance協(xié)議,我們可以進行全局配置:

// AppDelegate 進行全局設(shè)置
    if (@available(iOS 11.0, *)){
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }

這樣一來使用UITableview 、UICollectionView、UIScrollview的時候就不需要再單獨設(shè)置該屬性了。

3. 導航欄按鈕位置問題

之前這樣寫控制按鈕的邊距

    //調(diào)整按鈕邊距
//    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//    //將寬度設(shè)為負值
//    spaceItem.width= -5;
//    [items addObject:spaceItem];

今日不同往日,此方法無效了。

我試著使用了下面的方法

#pragma mark ————— 導航欄 添加文字按鈕 —————
- (NSMutableArray *)addNavigationItemWithTitles:(NSArray *)titles isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags
{
     
    NSMutableArray * items = [[NSMutableArray alloc] init];
     
    //調(diào)整按鈕位置
//    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//    //將寬度設(shè)為負值
//    spaceItem.width= -5;
//    [items addObject:spaceItem];
     
    NSMutableArray * buttonArray = [NSMutableArray array];
    NSInteger i = 0;
    for (NSString * title in titles) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 30, 30);
        [btn setTitle:title forState:UIControlStateNormal];
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = SYSTEMFONT(16);
        [btn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        btn.tag = [tags[i++] integerValue];
        [btn sizeToFit];
         
        //設(shè)置偏移
        if (isLeft) {
            [btn setContentEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
        }else{
            [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, -10)];
        }
         
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [items addObject:item];
        [buttonArray addObject:btn];
    }
    if (isLeft) {
        self.navigationItem.leftBarButtonItems = items;
    } else {
        self.navigationItem.rightBarButtonItems = items;
    }
    return buttonArray;
}

圖層調(diào)試發(fā)現(xiàn)此法其實屬障眼法,并不完美,設(shè)置內(nèi)容偏移,其實際位置并沒有發(fā)生變化,這可能導致按鈕部分區(qū)域無法點擊,目前偏移10像素問題不大,其他請自行測試,若有更完美的辦法請聯(lián)系我更新。

4. 位置權(quán)限

在IOS11,原有的NSLocationAlwaysUsageDeion被降級為NSLocationWhenInUseUsageDeion。因此,在原來項目中使用requestAlwaysAuthorization獲取定位權(quán)限,而未在plist文件中配置NSLocationAlwaysAndWhenInUseUsageDeion,系統(tǒng)框不會彈出。建議新舊key值都在plist里配置,反正我試下來是沒有問題,唯一的區(qū)別是使用requestAlwaysAuthorization獲取權(quán)限 IOS11系統(tǒng)彈框會把幾種權(quán)限級別全部列出,供用戶選擇,顯然更人性化了。

快去更新你的info.plist

   NSLocationUsageDescription
   獲取地理位置,精準推送服務(wù)
   NSLocationWhenInUseUsageDescription
   獲取地理位置,精準推送服務(wù)
   NSLocationAlwaysUsageDescription
   App需要您的同意,才能始終訪問位置
   NSLocationAlwaysAndWhenInUseUsageDeion
   App需要您的同意,才能始終訪問位置

iPhone X 適配

1.動態(tài)計算statusbar + navigationBar高度

送你幾個宏,來日好好擼,莫偷懶

#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define kNavBarHeight 44.0
#define kTabBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)
#define kTopHeight (kStatusBarHeight + kNavBarHeight)

這樣可以解決大部分因位置導致的問題

2.UISearchbar高度在ios11上由原來的44變成56
3.iPhoneX啟動圖 適配 (iPhone X 屏幕上下有黑道,沒有沾滿全屏)

之前的APP在iPhoneX屏幕沒填充滿,上下有黑色區(qū)域,應(yīng)該是你的app之前未用LaunchScreen.Storyboard作為啟動頁面,可以使用LaunchScreen來當做入場頁面,這樣APP才會自動適配為iPhoneX的大小?;蛘咝陆ㄒ粋€Assets中的LaunchImage來代替原來的,添加iPhoneX的尺寸圖(1125*2436)。

1518951-b00d1f10948e2dfd.png
4.MJRefresh的footer出現(xiàn)在Home Indicator位置上
1706847-895281fe29e07626.png

這是應(yīng)為iOS11棄用了automaticallyAdjustsScrollViewInsets屬性,新增contentInsetAdjustmentBehavior來替代它。就是iOS11會自動限制CollectionView的底部不會超出安全區(qū)范圍,不想系統(tǒng)幫我們自動設(shè)置邊距則要在iOS11中加上下面這句

if (@available(iOS 11.0, *)) {
        self.myCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }

但是這時候就會出現(xiàn)我們的CollectionView內(nèi)容會超出安全區(qū)和Home Indicator重疊,這樣最底下的cell兩邊就會被屏幕圓角切掉一部分,影響用戶體驗。我的解決辦法是給collectionView加上一個34pt高的footer

//iPhoneX是判斷機型的宏:#define iPhoneX ([UIScreen mainScreen].bounds.size.height == 812.0)
//SCREEN_WIDTH是屏幕寬宏:#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
if (iPhoneX) {
    flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 34);
  }
5.safeArea安全區(qū)設(shè)置無效

這是個很奇怪的坑,我的安全區(qū)設(shè)置無效是因為項目里使用了表情云的SDK,移除該SDK或更新至適配ios11的SDK即可。

參考:http://www.cocoachina.com/ios/20170925/20642.html
http://www.itdecent.cn/p/fd240dad1c2c

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容