10分鐘適配 iOS 11 & iPhone X

適配中的問題及解決辦法

1. 滾動條高度跳動、上下拉刷新問題:

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

2. 列表/頁面偏移

設置工程中的UITableViewUICollectionViewUIScrollViewcontentInsetAdjustmentBehavior屬性,以下:app

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

總的來講全部繼承與Scrollview 及其子類都須要設置 contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever ,每一個設置很麻煩,不要緊。因爲UIView及其子類都遵循UIAppearance協議,咱們能夠進行全局配置:ide

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

注意:spa

調用系統相冊時列表內容發生偏移能夠參考這篇文章 iOS11 系統適配中遇到的問題(1)UIImagePickerControllercode

3. 導航欄按鈕位置問題

iOS 11從新調整了導航欄的元素,強制將leftButtonsrightButtons位置往屏幕中央靠了一些,在這以前經過添加一個UIBarButtonSystemItemFixedSpace 把寬度設爲負數以調整按鈕的邊距orm

//調整按鈕邊距
//    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//    //將寬度設爲負值
//    spaceItem.width= -5;
//    [items addObject:spaceItem];

iOS 11如上設置是無效的
若是你沒法接受系統給設定的位置,能夠試試下面的方法xml

#pragma mark  導航欄 添加文字按鈕
- (NSMutableArray<UIButton *> *)addNavigationItemWithTitles:(NSArray *)titles isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags
{
    
    NSMutableArray * items = [[NSMutableArray alloc] init];
    
    //調整按鈕位置
//    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//    //將寬度設爲負值
//    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];
        
        //設置偏移
        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;
}

此法實屬障眼法,並不完美,經過設置內容偏移,讓按鈕視覺上看起來位置改變了,實際位置並無發生變化,這可能致使按鈕部分區域響應點擊事件。
若追求完美,能夠試着自定義UIButton重寫hitTest方法嘗試改變點擊區域。
如有其餘完美的辦法請聯繫我更新。繼承

4. 位置權限

在IOS11,原有的NSLocationAlwaysUsageDeion被降級爲NSLocationWhenInUseUsageDeion。所以,在原來項目中使用requestAlwaysAuthorization獲取定位權限,而未在plist文件中配置NSLocationAlwaysAndWhenInUseUsageDeion,系統框不會彈出。建議新舊key值都在plist裏配置,反正我試下來是沒有問題,惟一的區別是使用requestAlwaysAuthorization獲取權限 IOS11系統彈框會把幾種權限級別所有列出,供用戶選擇,顯然更人性化了。
快去更新你的info.plist事件

<!-- 位置 -->
    <key>NSLocationUsageDescription</key>
    <string>獲取地理位置,精準推送服務</string>
    <!-- 在使用期間訪問位置 -->
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>獲取地理位置,精準推送服務</string>
    <!-- 始終訪問位置 -->
    <key>NSLocationAlwaysUsageDescription</key>
    <string>App須要您的贊成,才能始終訪問位置</string>
    <!-- iOS 11訪問位置 -->
    <key>NSLocationAlwaysAndWhenInUseUsageDeion</key>
    <string>App須要您的贊成,才能始終訪問位置</string>

5. iPhone X 適配

iPhone X 變化最大的是頭部 & 底部
非iPhone X :
StatusBar 高20pt,NavigationBar 高44pt,底部TabBar高49pt
iPhone X:
StatusBar 高44pt,NavigationBar 高44pt,底部TabBar高83pt
因此,以前項目裏寫死的 ±49 ±64 都要出問題,若是你以前抽離出來使用的是宏,那問題不大,若是不是,開始搬磚吧少年。
送你幾個宏,來日好好擼,莫偷懶ip

#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define kNavBarHeight 44.0
//注意:請直接獲取系統的tabbar高度,若沒有用系統tabbar,建議判斷屏幕高度;以前判斷狀態欄高度的方法不妥,若是正在通話狀態欄會變高,致使判斷異常,下面只是一個例子,請勿直接使用!
#define kTabBarHeight kAppDelegate.mainTabBar.tabBar.frame.size.height
#define kTopHeight (kStatusBarHeight + kNavBarHeight)

替換 64pt →kTopHeight
替換 49pt →kTabBarHeightget

6. iPhone X push的時候TabBar上移

答案在這:適配iPhone X Push過程當中TabBar位置上移

這樣能夠解決大部分因位置致使的適配問題

請關注點❤️,持續更新……

 

以上屬於臭碼農原創,如有雷同屬巧合,若有錯誤望指正,轉載請標明來源和做者。
by:臭碼農

做者:臭碼農 連接:https://www.jianshu.com/p/94d3fdc0f20d 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。

相關文章
相關標籤/搜索