公司的App是居于iOS8以上的,頁面顯示在iOS8~iOS10都沒有問題,但是,iOS11beta版顯示出現(xiàn)各種問題,真是顧客虐你千百遍,你待顧客如初戀,蘋果搞事,我們也只能暗暗的承受。??
搞事一:導航欄
1.導航欄高度變化
導航欄在iOS10之前都是默認的64p,但是,到了iOS10就不單單是64p了,可以看一下系統(tǒng)的信息App,在iOS11添加了大標題,效果如下圖1:
navigationBar的結構,看圖2、3、4:
在上面三幅圖可以知道,在iOS11導航欄多了一個LargeTitleView,專門顯示大字標題用的,整個導航欄的高度達到了96p,這不包括狀態(tài)欄的高度,也就是說,整個app頂部高度達到了116p,其中statusbar=20,title=44,largetitle=52,不過默認是64p;當然,iPhoneX的高度會更高點,如果不顯示大字標題,頂部的高度也達到了88,statusbar=44,title=44,如果顯示大字標題,則高度變成了140,statusbar=44,title=44,largetitle=52,也就是說,iPhoneX的劉海高度為24p,大字標題如下圖:
2.導航欄的圖層變化
iOS11之前導航欄的title是添加在UINavigationItemView上面,而navigationBarButton則直接添加在navigationBar上面;如果設置了titleView,則titleView也是直接添加在navigationBar上面,如圖5:
在iOS11之后,蘋果添加了新的類來管理,navigationBar會添加在_UIButtonBarStackView上面,而_UIButtonBarStackView則添加在_UINavigationBarContentView上面;如果沒有給titleView賦值,則titleView會直接添加在_UINavigationBarContentView上面,如果賦值給了titleView,則會新生成_UITAMICAdaptorView,把titleView添加在這個類上面,這個類會添加在_UINavigationBarContentView上面,如下圖6、7:
3.導航欄的邊距變化
在iOS11對導航欄里面的item的邊距也做了調(diào)整:
(1)如果只是設置了titleView,沒有設置barbutton,把titleview的寬度設置為屏幕寬度,則titleview距離屏幕的邊距,iOS11之前,在iPhone6p上是20p,在iPhone6p之前是16p;iOS11之后,在iPhone6p上是12p,在iPhone6p之前是8p。
(2)如果只是設置了barbutton,沒有設置titleview,則在iOS11里,barButton距離屏幕的邊距是20p和16p;在iOS11之前,barButton距離屏幕的邊距也是20p和16p。
(3)如果同時設置了titleView和barButton,則在iOS11之前,titleview和barbutton之間的間距是6p,在iOS11上titleview和barbutton之間無間距,如下圖8、9:
4.App需要實現(xiàn)導航欄左右按鈕邊距為0
在iOS11之前,可以設置一個width為負的navigationBarButton,將按鈕擠到邊緣,變相實現(xiàn)0邊距的導航欄按鈕,但是,這招在iOS11失效了,原因在于_UIButtonBarStackView,這個iOS9之后出來的,用來相對布局的組件,限制了子view的布局。那怎么搞呢?
想到的方法有幾個:
(1)在viewWillAppear里面,將_UIButtonBarStackView取出來,直接設置它的x坐標。
(2)設置titleView,然后將button添加在titleView上面,根據(jù)不同的邊距做偏移。
方法一:
遇到的問題,在viewDidLoad,viewWillAppear,viewWillLayoutSubviews,viewDidLayoutSubviews里面都取不到_UIButtonBarStackView,只有在viewDidAppear里才能取到值,這樣就會在頁面顯示了之后才開始移動navigationBarButton,顯然這樣體驗不好,所以,暫時pass掉。
方法二:
這個做法完全可以做到0邊距,但是,問題來了,就是點擊區(qū)域的問題。因為左右navigationBarButton的點擊區(qū)域是超出父view的,所以,點擊不到。這好辦,重寫titleView的hitTest方法就好。嘿嘿嘿,問題沒有那么簡單。之前在iOS11的圖層結構就解釋過,titleView會被添加在_UITAMICAdaptorView上面,而重點是,這個view也有邊距,所以,單單重寫titleView的hitTest方法還不夠,那怎么解決呢?我的辦法就是寫一個view的類別,hook所有view的hitTest方法,在里面判斷是否是iOS11以上,是否是_UITAMICAdaptorView類,如果都滿足條件,則可以搞事了。??Demo
搞事二:列表的變化
1.automaticallyAdjustsScrollViewInsets
在iOS11之前,如果想要scrollView不偏移64p,則需設置automaticallyAdjustsScrollViewInsets=NO,但是這個屬性在iOS11直接被遺棄了??:
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets
API_DEPRECATED_WITH_REPLACEMENT("Use UIScrollView's contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0));
所以,看一下contentInsetAdjustmentBehavior是何方神圣:
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic, // 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
UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted
UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets
} API_AVAILABLE(ios(11.0),tvos(11.0));
/* Configure the behavior of adjustedContentInset.
Default is UIScrollViewContentInsetAdjustmentAutomatic.
*/
@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior API_AVAILABLE(ios(11.0),tvos(11.0));
看起來這和iOS11搞的safeArea有關,這個先放一遍,看看怎么適配:
#define adjustsScrollViewInsets_NO(scrollView,vc)\
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
if ([UIScrollView instancesRespondToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {\
[scrollView performSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:") withObject:@(2)];\
} else {\
vc.automaticallyAdjustsScrollViewInsets = NO;\
}\
_Pragma("clang diagnostic pop") \
} while (0)
上面是公司里面一個大神寫的,這樣就可以在Xcode8上面跑了。
2.tableView默認使用Self-Sizing
這個配合estimatedRowHeight、estimatedSectionFooterHeight、estimatedSectionHeaderHeight使用,可以預估高度。之前,設置header或者footer高度為0時,需要設置height=0.1,才會起作用,如果直接設置為0,則會使用默認高度。iOS11由于自動使用預估高度,所以,忽略了設置的高度,使原來的高度增大了。只要把這幾個屬性設置為0就可以解決。
搞事三:iPhoneX底部tabbar的高度改變
iPhoneX不止多了劉海,底部還有一個半角的矩形,使得tabbar多出來了34p的高度,不過不管導航欄和tabbar一般系統(tǒng)都會自動適配safeArea。
搞事四:iOS11 iPhoneX頁面push時tabbar位置變化
直接上圖:
可以看到在頁面push的時候,tabbar的frame上移了,這個只有在iPhoneX上面才能看到(因為iPhoneX的TabBar的高度不一樣),有可能是模擬器的bug,但是,具體要到真機出來才知道。下面說說修復的幾種辦法:
(1)將導航欄的代理設置為當前的controller,然后在將要展示下個頁面的方法里修正TabBar的frame。
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (![[[UIDevice currentDevice] modelName] isEqualToString: @"iPhone X"]) {
return;
}
CGRect frame = self.tabBarController.tabBar.frame;
if (frame.origin.y < ([UIScreen mainScreen].bounds.size.height - 83)) {
frame.origin.y = [UIScreen mainScreen].bounds.size.height - 83;
self.tabBarController.tabBar.frame = frame;
}
}
(2)新建一個類,繼承UITabBar,然后在setFrame:里面做判斷修正,將改類替換系統(tǒng)默認的TabBar。
- (void)setFrame:(CGRect)frame {
if ([[[UIDevice currentDevice] modelName] isEqualToString: @"iPhone X"]) {
if (frame.origin.y < ([UIScreen mainScreen].bounds.size.height - 83)) {
frame.origin.y = [UIScreen mainScreen].bounds.size.height - 83;
}
}
[super setFrame: frame];
}
(3)其他方法。
總結:
iOS11系統(tǒng)改變還是比較大的,某些地方需要注意適配,不然會出現(xiàn)很奇怪的現(xiàn)象。暫時,在iOS11遇到這么多坑,以后遇到會繼續(xù)分享的。