最近剛接手了個項目,在iOS11之前都沒有問題,但是在iOS11上卻出現了個別屏幕適配問題,其中包括:1、push進入下一個VC之后,導航欄在會往上移部分距離,大概20像素;2、VC中的tableView向下移動部分距離,以及cell直接的間隔會無故拉大;3、加載webView的時候會向下移動部分距離;4、放在導航欄上面的searchBar消失不見。雖然網上很多文章介紹解決的方法,但是我還是查閱了大部分簡書,博客,也花費了差不多兩天時間才把這個bug解決完。下面是出現bug界面的圖片,希望對你們能有所幫助。
1
2
3
其實解決這些bug很簡單,只不過不同的人遇到的問題不同罷了,至于為什么會出現這些bug,你們可以去官方或者大神的簡書去看看iOS界面布局的一些改變。現在就針對我們項目當中出現的問題,我一一給出答案,有不懂的,可以私信我。
1、導航欄向上跑了部分距離:宏定義一個高度
#define NAVIGATION_HEIGHT (CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]) + CGRectGetHeight(self.navigationController.navigationBar.frame))
在你設置的self.navigationBar.frame = CGRectMake(0, 0,ScreenWidth, NAVIGATION_HEIGHT);下面添加
#ifdef __IPHONE_11_0
if (@available(iOS 11.0, *)) {
self.navigationBar.frame = CGRectMake(0, STATUSBAR_HEIGHT,ScreenWidth, NAVIGATION_HEIGHT);
}
#endif
2、VC中的tableView向下移動部分距離,以及cell直接的間隔會無故拉大:
//在你的tableView下面添加這句話
if (@available(iOS 11.0, *)) {
UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
//如果你的cell 之間的間距拉大,就在self.xf_tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);這個約束后面添加下面三個約束
self.xf_tableView.estimatedRowHeight = 0;
self.xf_tableView.estimatedSectionHeaderHeight = 0;
self.xf_tableView.estimatedSectionFooterHeight = 0;
3、加載webView的時候會向下移動部分距離:給你的web添加下面約束
if (@available(iOS 11.0, *)) {
webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
4、放在導航欄上面的searchBar消失不見:
之前我的代碼是這樣寫的:
// 創建搜索框
UIView *titleView = [[UIView alloc] init];
titleView.py_x = PYSEARCH_MARGIN * 0.5;
titleView.py_y = 7;
titleView.py_width = self.view.py_width - 64 - titleView.py_x * 2;
titleView.py_height = 30;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds];
[titleView addSubview:searchBar];
self.navigationItem.titleView = titleView;
這樣你會發現搜索框不顯示,更改后的代碼是將titleView的UIView重寫為TUIView
新建一個TUIView類,在該類的.m里面實現以下方法:
#import "TUIView.h"
@implementation TUIView
-(CGSize)intrinsicContentSize
{
return UILayoutFittingExpandedSize;
}
@end
// 創建搜索框
UIView *titleView = [[TUIView alloc] init];
titleView.py_x = PYSEARCH_MARGIN * 0.5;
titleView.py_y = 7;
titleView.py_width = self.view.py_width - 64 - titleView.py_x * 2;
titleView.py_height = 30;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds];
[titleView addSubview:searchBar];
self.navigationItem.titleView = titleView;
這樣搜索框就顯示出來了。
這幾個bug著實讓我浪費了好多時間,希望我寫下這片文章對你們有多幫助