iOS開(kāi)發(fā)中適配iOS13新增的暗黑模式

1.顏色的適配

問(wèn)題:
iOS13下公司App某些頁(yè)面顯示情況:


0.暗黑.png

這是在暗黑模式下顯示的情況,出現(xiàn)這種情況是因?yàn)樵擁?yè)面的tableview和Cell都沒(méi)有設(shè)置背景色,導(dǎo)致系統(tǒng)自動(dòng)按照系統(tǒng)的顯示外觀對(duì)頁(yè)面顏色進(jìn)行了設(shè)置。

禁用App的暗黑模式

由于我們沒(méi)有對(duì)iOS13新增的暗黑模式進(jìn)行適配,我們可以禁用App的暗黑模式。
在info.plist文件中增加User Interface Style并設(shè)置為L(zhǎng)ight。


0.禁用.png
禁用App某些頁(yè)面的暗黑模式

如果我們對(duì)App某些頁(yè)面適配了暗黑模式,有些頁(yè)面還沒(méi)來(lái)得及適配,我們可以對(duì)某些頁(yè)面禁用暗黑模式。

  self.view.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;

適配顏色

iOS13設(shè)置顏色增加了方法

/* Create a dynamic color with a provider.
 * When methods are called on this color that need color component values,
 * the provider is called with UITraitCollection.currentTraitCollection.
 * The provider should use that trait collection to decide a more fundamental UIColor to return.
 * As much as possible, use the given trait collection to make that decision, not other state.
 */

+ (UIColor *)colorWithDynamicProvider:(UIColor * (^)(UITraitCollection     *traitCollection))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
- (UIColor *)initWithDynamicProvider:(UIColor * (^)(UITraitCollection *traitCollection))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);

我們可以通過(guò)block回調(diào)判斷當(dāng)前的模式,然后設(shè)置不同模式下的顏色。

  // 顏色適配
if (@available(iOS 13.0, *) ) {
    self.view.backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
        if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
            return [UIColor blackColor];    // 暗黑模式下的顏色
        }else{
            return [UIColor whiteColor];    // 非暗黑模式下的顏色
        }
    }];
}

UIUserInterfaceStyle是一個(gè)枚舉,有暗黑模式和淺色模式。

typedef NS_ENUM(NSInteger, UIUserInterfaceStyle) {
UIUserInterfaceStyleUnspecified,
UIUserInterfaceStyleLight,
UIUserInterfaceStyleDark,
} API_AVAILABLE(tvos(10.0)) API_AVAILABLE(ios(12.0)) API_UNAVAILABLE(watchos);

2.圖片的適配

0.圖片.png

我們可以選擇Appearances為Any,Dark,這樣就會(huì)出現(xiàn)兩組圖片,我們可以設(shè)置暗黑模式下的圖片和非暗黑模式下的圖片。

(個(gè)人感覺(jué)如果要適配暗黑模式要增加好多代碼?。??)

本篇文章到這里就結(jié)束了,愿大家加班不多工資多,男同胞都有女朋友,女同胞都有男朋友。??

?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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