iOS15適配

UINavigationBar

記得一定要 iOS15 和已以前的版本都好好測(cè)一下,適配的時(shí)候一定要保留之前的導(dǎo)航欄的配置?。?!

用新 xcode13 編譯工程后,導(dǎo)航欄的問題比較明顯,調(diào)試之后發(fā)現(xiàn)是 UINavigationBar 部分屬性的設(shè)置在 iOS15 上是無(wú)效的

查看導(dǎo)航欄特性 API:UINavigationBarAppearance 后發(fā)現(xiàn),iOS15navigationBar 的相關(guān)屬性設(shè)置要通過實(shí)例 UINavigationBarAppearance 來(lái)實(shí)現(xiàn),UINavigationBarAppearance 是 iOS13 更新的 API,應(yīng)該有人已經(jīng)在用,我們的應(yīng)用兼容 iOS10 以上,對(duì)于導(dǎo)航欄的設(shè)置還沒有使用 UINavigationBarAppearance,如今在 iOS15 上失效,所以對(duì)于呈現(xiàn)的問題,做如下適配:

解決方法

主要是以下兩個(gè)屬性 (UINavigationController 的屬性)

// 靜止樣式self.navigationBar.standardAppearance;// 滾動(dòng)樣式self.navigationBar.scrollEdgeAppearance;

下面只列了 UINavigationController 主要處理代碼

swift

if#available(iOS13.0,*){

let appearance=UINavigationBarAppearance()// 設(shè)置導(dǎo)航欄背景色

appearance.backgroundColor=.white// 去除導(dǎo)航欄陰影(如果不設(shè)置clear,導(dǎo)航欄底下會(huì)有一條陰影線)

appearance.shadowColor=UIColor.clear// 字體顏色、尺寸等

appearance.titleTextAttributes=[NSAttributedString.Key.foregroundColor:UIColor.white]// 帶scroll滑動(dòng)的頁(yè)面

navigationController?.navigationBar.scrollEdgeAppearance=appearance// 常規(guī)頁(yè)面navigationController?.navigationBar.standardAppearance=appearance

}

Objective-C

if(@available(iOS13.0,*)){

????UINavigationBarAppearance*appearance=[[UINavigationBarAppearance alloc]init];// 背景色????appearance.backgroundColor=[UIColor whiteColor];// 去除導(dǎo)航欄陰影(如果不設(shè)置clear,導(dǎo)航欄底下會(huì)有一條陰影線)

????appearance.shadowColor=[UIColor clearColor];// 字體顏色、尺寸等????

appearance.titleTextAttributes=@{? ?NSForegroundColorAttributeName:[UIColor redColor]};// 帶scroll滑動(dòng)的頁(yè)面

self.navigationController.navigationBar.scrollEdgeAppearance=appearance;// 常規(guī)頁(yè)面self.navigationController.navigationBar.standardAppearance=appearance;

}

appdelegate全局設(shè)置

(代碼大差不差,此處就只列出 oc 的代碼)之前有人遇到導(dǎo)航欄隱藏的返回按鈕失效問題,備注里面也已經(jīng)解決,并做出說(shuō)明

[[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(-200,0)forBarMetrics:UIBarMetricsDefault];

// iOS 15適配

if(@available(iOS13.0,*)){

????UINavigationBarAppearance*appearance=[[UINavigationBarAppearance alloc]init];

????????[appearance setBackgroundColor:[UIColor whiteColor]];// UINavigationBarAppearance 會(huì)覆蓋原有的導(dǎo)航欄設(shè)置,這里需要重新設(shè)置返回按鈕隱藏,不隱藏可注釋或刪掉appearance.backButtonAppearance.normal.titlePositionAdjustment=UIOffsetMake(-200,0);

[[UINavigationBar appearance]setScrollEdgeAppearance:appearance];

[[UINavigationBar appearance]setStandardAppearance:appearance];

}

UITabbar

tabbar 的問題和 navigationBar 的問題屬于同一類,tabbar 背景顏色設(shè)置失效

swift

if#available(iOS13.0,*){

let appearance=UITabBarAppearance()// 背景色appearance.backgroundColor=.white? ? ? ? ? ? tabBar.standardAppearance=appearance

if#available(iOS15.0,*){

????tabBar.scrollEdgeAppearance=appearance

????}

}

Objective-C

if(@available(iOS13.0,*)){

UITabBarAppearance*appearance=[[UITabBarAppearance alloc]init];// 背景色appearance.backgroundColor=[UIColor whiteColor];

self.tabBar.standardAppearance=appearance;

if(@available(iOS15.0,*)){self.tabBar.scrollEdgeAppearance=appearance;}

}



TableView

iOS 15 的 UITableView 新增了一條新屬性:sectionHeaderTopPadding, 默認(rèn)會(huì)給每一個(gè) section header 增加一個(gè)高度,當(dāng)我們使用 UITableViewStylePlain 初始化 UITableView 的時(shí)候,能發(fā)現(xiàn) sectionHeader 增高了 22px。

/// Padding above each section header. The default value is `UITableViewAutomaticDimension`.@available(iOS15.0,*)openvarsectionHeaderTopPadding:CGFloat//iOS 15中tableView會(huì)給每一個(gè)section的頂部(header以上)再加上一個(gè)22像素的高度,形成一個(gè)section和section之間的間距

swift

//為了配合以前的開發(fā)習(xí)慣,我們只需要在創(chuàng)建實(shí)例的時(shí)候進(jìn)行對(duì)間距的設(shè)置即可if#available(iOS15.0,*){tableView.sectionHeaderTopPadding=0}//或者全局設(shè)置if#available(iOS15.0,*){UITableView.appearance().sectionHeaderTopPadding=0}

Objective-C

//為了配合以前的開發(fā)習(xí)慣,我們只需要在創(chuàng)建實(shí)例的時(shí)候進(jìn)行對(duì)間距的設(shè)置即可i

f(@available(iOS15.0,*)){tableView.sectionHeaderTopPadding=0;}//或者全局設(shè)置 if(@available(iOS15.0,*)){[UITableView appearance].sectionHeaderTopPadding=0;}

有用請(qǐng)點(diǎn)個(gè)贊 再走吧 么么噠

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容