ios11適配中的問題及解決辦法
1. 滾動條高度跳動、上下拉刷新問題:
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
2. 列表/頁面偏移
本來是這樣的
if (@available(iOS 11.0, *)){
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
目前發現所有的Scrollview 及其子類都需要設置 contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever ,工程中大量使用列表的同學不要慌,不要忙,因為UIView及其子類都遵循UIAppearance協議,我們可以進行全局配置:
// AppDelegate 進行全局設置
if (@available(iOS 11.0, *)){
[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
這樣一來使用UITableview 、UICollectionView、UIScrollview的時候就不需要再單獨設置該屬性了。
3. 導航欄按鈕位置問題
之前這樣寫控制按鈕的邊距
//調整按鈕邊距
// UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// //將寬度設為負值
// 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];
//調整按鈕位置
// 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;
}
圖層調試發現此法其實屬障眼法,并不完美,設置內容偏移,其實際位置并沒有發生變化,這可能導致按鈕部分區域無法點擊,目前偏移10像素問題不大,其他請自行測試,若有更完美的辦法請聯系我更新。
4. 位置權限
在IOS11,原有的NSLocationAlwaysUsageDeion被降級為NSLocationWhenInUseUsageDeion。因此,在原來項目中使用requestAlwaysAuthorization獲取定位權限,而未在plist文件中配置NSLocationAlwaysAndWhenInUseUsageDeion,系統框不會彈出。建議新舊key值都在plist里配置,反正我試下來是沒有問題,唯一的區別是使用requestAlwaysAuthorization獲取權限 IOS11系統彈框會把幾種權限級別全部列出,供用戶選擇,顯然更人性化了。
快去更新你的info.plist
NSLocationUsageDescription
獲取地理位置,精準推送服務
NSLocationWhenInUseUsageDescription
獲取地理位置,精準推送服務
NSLocationAlwaysUsageDescription
App需要您的同意,才能始終訪問位置
NSLocationAlwaysAndWhenInUseUsageDeion
App需要您的同意,才能始終訪問位置
iPhone X 適配
1.動態計算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屏幕沒填充滿,上下有黑色區域,應該是你的app之前未用LaunchScreen.Storyboard作為啟動頁面,可以使用LaunchScreen來當做入場頁面,這樣APP才會自動適配為iPhoneX的大小。或者新建一個Assets中的LaunchImage來代替原來的,添加iPhoneX的尺寸圖(1125*2436)。
4.MJRefresh的footer出現在Home Indicator位置上
這是應為iOS11棄用了automaticallyAdjustsScrollViewInsets屬性,新增contentInsetAdjustmentBehavior來替代它。就是iOS11會自動限制CollectionView的底部不會超出安全區范圍,不想系統幫我們自動設置邊距則要在iOS11中加上下面這句
if (@available(iOS 11.0, *)) {
self.myCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
但是這時候就會出現我們的CollectionView內容會超出安全區和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安全區設置無效
這是個很奇怪的坑,我的安全區設置無效是因為項目里使用了表情云的SDK,移除該SDK或更新至適配ios11的SDK即可。
參考:http://www.cocoachina.com/ios/20170925/20642.html
http://www.lxweimin.com/p/fd240dad1c2c