這篇文章將為大家詳細(xì)講解有關(guān)iOS11、iPhone X、Xcode9如何適配,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
更新iOS11后,發(fā)現(xiàn)有些地方需要做適配,整理后按照優(yōu)先級(jí)分為以下三類(lèi):
單純升級(jí)iOS11后造成的變化;
Xcode9 打包后造成的變化;
iPhoneX的適配
一、單純升級(jí)iOS11后造成的變化
升級(jí)后,發(fā)現(xiàn)某個(gè)擁有tableView的界面錯(cuò)亂,組間距和contentInset錯(cuò)亂,因?yàn)閕OS11中 UIViewController 的 automaticallyAdjustsScrollViewInsets 屬性被廢棄了,因此當(dāng)tableView超出安全區(qū)域時(shí),系統(tǒng)自動(dòng)會(huì)調(diào)整SafeAreaInsets值,進(jìn)而影響adjustedContentInset值
// 有些界面以下使用代理方法來(lái)設(shè)置,發(fā)現(xiàn)并沒(méi)有生效 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; // 這樣的原理是因?yàn)橹爸皇菍?shí)現(xiàn)了高度的代理方法,卻沒(méi)有實(shí)現(xiàn)View的代理方法,iOS10及以前這么寫(xiě)是沒(méi)問(wèn)題的,iOS11開(kāi)啟了行高估算機(jī)制引起的bug,因此有以下幾種解決方法: // 解決方法一:添加實(shí)現(xiàn)View的代理方法 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { return nil; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return nil; } // 解決方法二:直接使用tableView屬性進(jìn)行設(shè)置,修復(fù)該UI錯(cuò)亂 self.tableView.sectionHeaderHeight = 0; self.tableView.sectionFooterHeight = 5; [_optionTableView setContentInset:UIEdgeInsetsMake(-35, 0, 0, 0)]; // 解決方法三:添加以下代碼關(guān)閉估算行高 self.tableView.estimatedRowHeight = 0; self.tableView.estimatedSectionHeaderHeight = 0; self.tableView.estimatedSectionFooterHeight = 0;
四、使用Xcode9 編譯后發(fā)現(xiàn)的問(wèn)題
1. 發(fā)現(xiàn)“fastSocket”第三方報(bào)錯(cuò),具體原因是缺少C99的頭文件,引入“#include <sys/time.h>”即可
2. 導(dǎo)航欄的新特性
原生的搜索欄樣式發(fā)生改變
右邊為iOS11樣式,搜索區(qū)域高度變大,字體變大
查看 API 后發(fā)現(xiàn),iOS11后將 searchController 賦值給了 NavigationItem,通過(guò)屬性 hidesSearchBarWhenScrolling 可以控制搜索欄是否在滑動(dòng)的時(shí)候進(jìn)行隱藏和顯示
// A view controller that will be shown inside of a navigation controller can assign a UISearchController to this property to display the search controller's search bar in its containing navigation controller's navigation bar. @property (nonatomic, retain, nullable) UISearchController *searchController API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos); // If this property is true (the default), the searchController's search bar will hide as the user scrolls in the top view controller's scroll view. If false, the search bar will remain visible and pinned underneath the navigation bar.
另外,UINavigationBar 新增屬性 BOOL值 prefersLargeTitles 來(lái)實(shí)現(xiàn)下面的效果,并可以通過(guò) largeTitleTextAttributes 來(lái)設(shè)置大標(biāo)題的文本樣式
有個(gè)界面使用到了導(dǎo)航欄按鈕相關(guān)的frame,也發(fā)生了UI錯(cuò)亂,查看UI層級(jí)關(guān)系后發(fā)現(xiàn),iOS11以前是直接把按鈕加到了UINavigationBar上面,而iOS11則是先將按鈕加到了_UITAMICAdaptorView,再加到_UIButtonBarStackView、_UINavigationBarContentView,接著才是UINavigationBar。因此如果需要獲取導(dǎo)航欄按鈕 frame 或者 superView,這里需要專(zhuān)門(mén)做下適配
iOS10及以下版本導(dǎo)航欄按鈕層級(jí)關(guān)系圖
iOS11導(dǎo)航欄按鈕層級(jí)關(guān)系圖
三、iPhone X的適配
下載完Xcode9之后,第一件事自然是在 iPhone X(模擬器)上過(guò)把癮,然后編譯后就發(fā)現(xiàn)報(bào)錯(cuò)了
由于iPhone X的狀態(tài)欄是和其他版本手機(jī)差異比較大的,因此api 變化也比較大
先后做了以下適配
適配點(diǎn)一:項(xiàng)目中使用狀態(tài)欄中圖標(biāo)判斷當(dāng)前網(wǎng)絡(luò)的具體狀態(tài)
出錯(cuò)代碼
打印的 Log 報(bào)出以下錯(cuò)誤: Trapped uncaught exception 'NSUnknownKeyException', reason: '[<UIStatusBar_Modern 0x7fcdb0805770> valueForUndefinedKey:]: this class is not key value coding-compliant for the key foregroundView.'
iPhone X
其他手機(jī)
使用 runtime 打印其所有屬性,發(fā)現(xiàn)以下差異
// 測(cè)試代碼 #import <objc/runtime.h> NSMutableString *resultStr = [NSMutableString string]; //獲取指定類(lèi)的Ivar列表及Ivar個(gè)數(shù) unsigned int count = 0; Ivar *member = class_copyIvarList([[application valueForKeyPath:@"_statusBar"] class], &count); for(int i = 0; i < count; i++){ Ivar var = member[i]; //獲取Ivar的名稱(chēng) const char *memberAddress = ivar_getName(var); //獲取Ivar的類(lèi)型 const char *memberType = ivar_getTypeEncoding(var); NSString *str = [NSString stringWithFormat:@"key = %s type = %s \n",memberAddress,memberType]; [resultStr appendString:str]; } NSLog(@"%@", resultStr);
// 其他版本的手機(jī) key = _inProcessProvider type = @"<UIStatusBarStateProvider>" key = _showsForeground type = B key = _backgroundView type = @"UIStatusBarBackgroundView" key = _doubleHeightLabel type = @"UILabel" key = _doubleHeightLabelContainer type = @"UIView" key = _currentDoubleHeightText type = @"NSString" key = _currentRawData type = {超長(zhǎng)。。} key = _interruptedAnimationCompositeViews type = @"NSMutableArray" key = _newStyleBackgroundView type = @"UIStatusBarBackgroundView" key = _newStyleForegroundView type = @"UIStatusBarForegroundView" key = _slidingStatusBar type = @"UIStatusBar" key = _styleAttributes type = @"UIStatusBarStyleAttributes" key = _waitingOnCallbackAfterChangingStyleOverridesLocally type = B key = _suppressGlow type = B key = _translucentBackgroundAlpha type = d key = _showOnlyCenterItems type = B key = _foregroundViewShouldIgnoreStatusBarDataDuringAnimation type = B key = _tintColor type = @"UIColor" key = _lastUsedBackgroundColor type = @"UIColor" key = _nextTintTransition type = @"UIStatusBarStyleAnimationParameters" key = _overrideHeight type = @"NSNumber" key = _disableRasterizationReasons type = @"NSMutableSet" key = _timeHidden type = B key = _statusBarWindow type = @"UIStatusBarWindow" // iPhone X key = _statusBar ; type = @"_UIStatusBar" // 因此可見(jiàn)iPhone X的狀態(tài)欄是多嵌套了一層,多取一次即可,最終適配代碼為: NSArray *children; // 不能用 [[self deviceVersion] isEqualToString:@"iPhone X"] 來(lái)判斷,因?yàn)槟M器不會(huì)返回 iPhone X if ([[application valueForKeyPath:@"_statusBar"] isKindOfClass:NSClassFromString(@"UIStatusBar_Modern")]) { children = [[[[application valueForKeyPath:@"_statusBar"] valueForKeyPath:@"_statusBar"] valueForKeyPath:@"foregroundView"] subviews]; } else { children = [[[application valueForKeyPath:@"_statusBar"] valueForKeyPath:@"foregroundView"] subviews]; }
適配點(diǎn)二:解決這個(gè)問(wèn)題后項(xiàng)目跑起來(lái)發(fā)現(xiàn),整個(gè)app界面上下各空出大概40pt的高度
經(jīng)常從 Github 上下載項(xiàng)目把玩的老司機(jī)們都知道,有些老項(xiàng)目在模擬器上跑起來(lái)之后也會(huì)只有 iPhone 4(320*480)的布局空間,造成這個(gè)的原因是啟動(dòng)圖使用 Launch Images Source 設(shè)置的時(shí)候沒(méi)有勾選并設(shè)置對(duì)應(yīng)的圖片,解決方法如下
然而iPhone X更大的坑是屏幕的適配
首先看下屏幕尺寸
這張圖反映出不少信息:
iPhone X的寬度雖然和7是一樣的,但是高度多出145pt
使用三倍圖是重點(diǎn),而且一般認(rèn)為肉眼所能所能識(shí)別的最高的屏幕密度是300ppi,iPhone X已達(dá)到458ppi(查證發(fā)現(xiàn)三星galaxy系列的屏幕密度是522ppi)
在設(shè)計(jì)方面,蘋(píng)果官方文檔human-interface-guidelines有明確要求,下面結(jié)合圖例進(jìn)行說(shuō)明:
展示出來(lái)的設(shè)計(jì)布局要求填滿整個(gè)屏幕
填滿的同時(shí)要注意控件不要被大圓角和傳感器部分所遮擋
安全區(qū)域以外的部分不允許有任何與用戶(hù)交互的控件
上面這張圖內(nèi)含信息略多
頭部導(dǎo)航欄不予許進(jìn)行用戶(hù)交互的,意味著下面這兩種情況 Apple 官方是不允許的
底部虛擬區(qū)是替代了傳統(tǒng)home鍵,高度為34pt,通過(guò)上滑可呼起多任務(wù)管理,考慮到手勢(shì)沖突,這部分也是不允許有任何可交互的控件
狀態(tài)欄在非安全區(qū)域,文檔中也提到,除非可以通過(guò)隱藏狀態(tài)欄給用戶(hù)帶來(lái)額外的價(jià)值,否則最好把狀態(tài)欄還給用戶(hù)
關(guān)于“iOS11、iPhone X、Xcode9如何適配”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.rwnh.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)題目:iOS11、iPhoneX、Xcode9如何適配-創(chuàng)新互聯(lián)
鏈接URL:http://www.rwnh.cn/article44/dcehee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、商城網(wǎng)站、域名注冊(cè)、移動(dòng)網(wǎng)站建設(shè)、虛擬主機(jī)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容