欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

iOS13適配深色模式(DarkMode)的實現(xiàn)

好像大概也許是一年前, Mac OS系統(tǒng)發(fā)布了深色模式外觀, 看著挺刺激, 時至今日用著也還挺爽的

站在用戶的角度思考問題,與客戶深入溝通,找到三水網(wǎng)站設計與三水網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、國際域名空間、虛擬主機、企業(yè)郵箱。業(yè)務覆蓋三水地區(qū)。

終于, 隨著iPhone11等新手機的發(fā)售, iOS 13系統(tǒng)也正式發(fā)布了, 伴隨著手機版的深色模式也出現(xiàn)在了大眾視野

我們這些iOS程序猿也有事情做了, 原有項目適配iOS13系統(tǒng), 適配Dark Mode深色模式

雖然現(xiàn)在并沒有要求強制適配Dark Mode, 但是DarK適配卻也迫在眉睫

Apps on iOS 13 are expected to support dark mode Use system colors and materials Create your own dynamic colors and images Leverage flexible infrastructure

獲取當前模式

提供兩種方式設置手機當前外觀模式

  • 設置 --> 顯示與亮度
  • 控制中心, 長按亮度調節(jié)按鈕

獲取當前模式

我們需要選獲取到當前出于什么模式, 在根據(jù)不同的模式進行適配, iOS 13中新增了獲取當前模式的API

Swift

// 獲取當前模式
let currentMode = UITraitCollection.current.userInterfaceStyle
if (currentMode == .dark) {
 print("深色模式")
} else if (currentMode == .light) {
 print("淺色模式")
} else {
 print("未知模式")
}

 
open var userInterfaceStyle: UIUserInterfaceStyle { get } 

// 所有模式
public enum UIUserInterfaceStyle : Int {
 // 未指明的
 case unspecified
 // 淺色模式
 case light
 // 深色模式
 case dark
}

OC語言

if (@available(iOS 13.0, *)) {
 UIUserInterfaceStyle mode = UITraitCollection.currentTraitCollection.userInterfaceStyle;
 if (mode == UIUserInterfaceStyleDark) {
  NSLog(@"深色模式");
 } else if (mode == UIUserInterfaceStyleLight) {
  NSLog(@"淺色模式");
 } else {
  NSLog(@"未知模式");
 }
}

// 各種枚舉值
typedef NS_ENUM(NSInteger, UIUserInterfaceStyle) {
 UIUserInterfaceStyleUnspecified,
 UIUserInterfaceStyleLight,
 UIUserInterfaceStyleDark,
} API_AVAILABLE(tvos(10.0)) API_AVAILABLE(ios(12.0)) API_UNAVAILABLE(watchos);

監(jiān)聽系統(tǒng)模式的變化

在iOS13系統(tǒng)中, UIViewController遵循了兩個協(xié)議: UITraitEnvironmentUIContentContainer協(xié)議

UITraitEnvironment協(xié)議中, 為我們提供了一個監(jiān)聽當前模式變化的方法

@protocol UITraitEnvironment <NSObject>
// 當前模式
@property (nonatomic, readonly) UITraitCollection *traitCollection API_AVAILABLE(ios(8.0));

// 重寫該方法監(jiān)聽模式的改變
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection API_AVAILABLE(ios(8.0));
@end
public protocol UITraitEnvironment : NSObjectProtocol {
 // 當前模式
 @available(iOS 8.0, *)
 var traitCollection: UITraitCollection { get }

 // 重寫該方法監(jiān)聽模式的改變
 @available(iOS 8.0, *)
 func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)
}


// 使用方法
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
 super.traitCollectionDidChange(previousTraitCollection)
 
 // 每次模式改變的時候, 這里都會執(zhí)行
 print("模式改變了")
}

顏色相關適配

  • 不同模式的適配主要涉及顏色和圖片兩個方面的適配
  • 其中顏色適配, 包括相關背景色和字體顏色
  • 當系統(tǒng)模式切換的時候, 我們不需要如何操作, 系統(tǒng)會自動渲染頁面, 只需要做好不同模式的顏色和圖片即可

UIColor

iOS13之前UIColor只能表示一種顏色,從iOS13開始UIColor是一個動態(tài)的顏色,在不同模式下可以分別代表不同的顏色

下面是iOS13系統(tǒng)提供的動態(tài)顏色種類, 使用以下顏色值, 在模式切換時, 則不需要做特殊處理

@interface UIColor (UIColorSystemColors)
#pragma mark System colors

@property (class, nonatomic, readonly) UIColor *systemRedColor   API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *systemGreenColor  API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *systemBlueColor   API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *systemOrangeColor  API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *systemYellowColor  API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *systemPinkColor   API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *systemPurpleColor  API_AVAILABLE(ios(9.0), tvos(9.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *systemTealColor   API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *systemIndigoColor  API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
// 灰色種類, 在Light模式下, systemGray6Color更趨向于白色
@property (class, nonatomic, readonly) UIColor *systemGrayColor   API_AVAILABLE(ios(7.0), tvos(9.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *systemGray2Color  API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *systemGray3Color  API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *systemGray4Color  API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *systemGray5Color  API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *systemGray6Color  API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);

#pragma mark Foreground colors
@property (class, nonatomic, readonly) UIColor *labelColor    API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *secondaryLabelColor  API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *tertiaryLabelColor  API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *quaternaryLabelColor API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
// 系統(tǒng)鏈接的前景色
@property (class, nonatomic, readonly) UIColor *linkColor    API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
// 占位文字的顏色
@property (class, nonatomic, readonly) UIColor *placeholderTextColor API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
// 邊框或者分割線的顏色
@property (class, nonatomic, readonly) UIColor *separatorColor   API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
@property (class, nonatomic, readonly) UIColor *opaqueSeparatorColor API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);

#pragma mark Background colors
@property (class, nonatomic, readonly) UIColor *systemBackgroundColor     API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *secondarySystemBackgroundColor   API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *tertiarySystemBackgroundColor   API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *systemGroupedBackgroundColor   API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *secondarySystemGroupedBackgroundColor API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *tertiarySystemGroupedBackgroundColor API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);

#pragma mark Fill colors
@property (class, nonatomic, readonly) UIColor *systemFillColor       API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *secondarySystemFillColor    API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *tertiarySystemFillColor     API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);
@property (class, nonatomic, readonly) UIColor *quaternarySystemFillColor    API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos, watchos);

#pragma mark Other colors
// 這兩個是非動態(tài)顏色值
@property(class, nonatomic, readonly) UIColor *lightTextColor API_UNAVAILABLE(tvos); // for a dark background
@property(class, nonatomic, readonly) UIColor *darkTextColor API_UNAVAILABLE(tvos);  // for a light background

@property(class, nonatomic, readonly) UIColor *groupTableViewBackgroundColor API_DEPRECATED_WITH_REPLACEMENT("systemGroupedBackgroundColor", ios(2.0, 13.0), tvos(13.0, 13.0));
@property(class, nonatomic, readonly) UIColor *viewFlipsideBackgroundColor API_DEPRECATED("", ios(2.0, 7.0)) API_UNAVAILABLE(tvos);
@property(class, nonatomic, readonly) UIColor *scrollViewTexturedBackgroundColor API_DEPRECATED("", ios(3.2, 7.0)) API_UNAVAILABLE(tvos);
@property(class, nonatomic, readonly) UIColor *underPageBackgroundColor API_DEPRECATED("", ios(5.0, 7.0)) API_UNAVAILABLE(tvos);

@end

上面系統(tǒng)提供的這些顏色種類, 根本不能滿足我們正常開發(fā)的需要, 大部分的顏色值也都是自定義

系統(tǒng)也為我們提供了創(chuàng)建自定義顏色的方法

@available(iOS 13.0, *)
public init(dynamicProvider: @escaping (UITraitCollection) -> UIColor)

在OC中

+ (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);
  • 上面的方法接受一個閉包(block)
  • 當系統(tǒng)在LightMode和DarkMode之間相互切換時就會自動觸發(fā)此回調
  • 回調返回一個UITraitCollection, 可根據(jù)改對象判斷是那種模式
fileprivate func getColor() -> UIColor {
 return UIColor { (collection) -> UIColor in
  if (collection.userInterfaceStyle == .dark) {
   return UIColor.red
  }
  return UIColor.green
 }
}

除了上述兩個方法之外, UIColor還增加了一個實例方法

// 通過當前traitCollection得到對應UIColor
@available(iOS 13.0, *)
open func resolvedColor(with traitCollection: UITraitCollection) -> UIColor

CGColor

  • UIColor只是設置背景色和文字顏色的類, 可以動態(tài)的設置
  • 可是如果是需要設置類似邊框顏色等屬性時, 又該如何處理呢
  • 設置上述邊框屬性, 需要用到CGColor類, 但是在iOS13中CGColor并不是動態(tài)顏色值, 只能表示一種顏色
  • 在監(jiān)聽模式改變的方法中traitCollectionDidChange, 根據(jù)不同的模式進行處理
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
 super.traitCollectionDidChange(previousTraitCollection)
 
 // 每次模式改變的時候, 這里都會執(zhí)行
 if (previousTraitCollection?.userInterfaceStyle == .dark) {
  redView.layer.borderColor = UIColor.red.cgColor
 } else {
  redView.layer.borderColor = UIColor.green.cgColor
 }
}

圖片適配

在iOS中, 圖片基本都是放在Assets.xcassets里面, 所以圖片的適配, 我們就相對麻煩一些了
正常情況下都是下面這中處理方式

iOS13適配深色模式(Dark Mode)的實現(xiàn)

需要適配不同模式的情況下, 需要兩套不同的圖片, 并做如下設置

在設置Appearances時, 我們選擇Any, Dark就可以了(只需要適配深色模式和非深色模式)

iOS13適配深色模式(Dark Mode)的實現(xiàn)

適配相關

當前頁面模式

原項目中如果沒有適配Dark Mode, 當你切換到Dark Mode后, 你可能會發(fā)現(xiàn), 有些部分頁面的顏色自動適配了
未設置過背景顏色或者文字顏色的組件, 在Dark Mode模式下, 就是黑色的
這里我們就需要真對該單獨App強制設置成Light Mode模式

// 設置改屬性, 只會影響當前的視圖, 不會影響前面的controller和后續(xù)present的controller
@available(iOS 13.0, *)
open var overrideUserInterfaceStyle: UIUserInterfaceStyle

// 使用示例
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
 super.traitCollectionDidChange(previousTraitCollection)
 
 // 每次模式改變的時候, 這里都會執(zhí)行
 if (previousTraitCollection?.userInterfaceStyle == .dark) {
  // 在Dark模式下, 強制改成Light模式
  overrideUserInterfaceStyle = .light
 }
}

強制項目的顯示模式

上面這種方式只能針對某一個頁面修改, 如果需要對整個項目禁用Dark模式

可以通過修改window的overrideUserInterfaceStyle屬性

在Xcode11創(chuàng)建的項目中, window從AppDelegate移到SceneDelegate中, 添加下面這段代碼, 就會做到全局修改顯示模式

let scene = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate
scene?.window?.overrideUserInterfaceStyle = .light

在之前的項目中, 可以在AppDelegate設置如下代碼

window.overrideUserInterfaceStyle = .light

我創(chuàng)建的簡單項目, 上述代碼的確會強制改變當前的模式, 但是狀態(tài)欄的顯示不會被修改, 不知道是不是漏了什么

終極方案

需要在info.plist文件中添加User Interface Style配置, 并設置為Light

<key>UIUserInterfaceStyle</key>
<string>Light</string>

問題又來了, 即使做了上面的修改, 在React Native中, 狀態(tài)欄的依然是根據(jù)不同的模式顯示不同的顏色, 該如何處理嘞?

Status Bar更新

在iOS13中蘋果對于Status Bar也做了部分修改, 在iOS13之前

public enum UIStatusBarStyle : Int {
 case `default` // 默認文字黑色

 @available(iOS 7.0, *)
 case lightContent // 文字白色
}

從iOS13開始UIStatusBarStyle一共有三種狀態(tài)

public enum UIStatusBarStyle : Int {
 case `default` // 自動選擇黑色或白色

 @available(iOS 7.0, *)
 case lightContent // 文字白色
 
 @available(iOS 13.0, *)
 case darkContent // 文字黑色
}

在React Native的代碼中, 設置狀態(tài)欄的顏色為黑色, 代碼如下

<StatusBar barStyle={'dark-content'} />

上面這段代碼在iOS13系統(tǒng)的手機中是無效的

雖然上面的代碼中設置了dark-content模式, 但是在iOS原生代碼中dark-content實際是UIStatusBarStyleDefault

在文件RCTStatusBarManager.m中

RCT_ENUM_CONVERTER(UIStatusBarStyle, (@{
 @"default": @(UIStatusBarStyleDefault),
 @"light-content": @(UIStatusBarStyleLightContent),
 @"dark-content": @(UIStatusBarStyleDefault),
}), UIStatusBarStyleDefault, integerValue);

修改上面代碼即可

@"dark-content": @(@available(iOS 13.0, *) ? UIStatusBarStyleDarkContent : UIStatusBarStyleDefault),

iOS13 其他更新

蘋果登錄

Sign In with Apple will be available for beta testing this summer. It will be required as an option for users in apps that support third-party sign-in when it is commercially available later this year.

如果APP支持三方登陸(Facbook、Google、微信、QQ、支付寶等),就必須支持蘋果登陸,且要放前邊
至于Apple登錄按鈕的樣式, 建議支持使用Apple提供的按鈕樣式,已經(jīng)適配各類設備, 可參考Sign In with Apple

LaunchImage

即將被廢棄的LaunchImage

  • 從iOS 8的時候,蘋果就引入了LaunchScreen,我們可以設置LaunchScreen來作為啟動頁。
  • 現(xiàn)在你還可以使用LaunchImage來設置啟動圖, 但是隨著蘋果設備尺寸越來越多, 適配顯然相對麻煩一些
  • 使用LaunchScreen的話,情況會變的很簡單,LaunchScreen是支持AutoLayout和SizeClass的,所以適配各種屏幕都不在話下。
  • ⚠️從2020年4月開始,所有App將必須提供LaunchScreen,而LaunchImage即將退出歷史舞臺

UIWebView

'UIWebView' was deprecated in iOS 12.0: No longer supported; please adopt WKWebView.

從iOS 13開始也不再支持UIWebView控件了, 盡快替換成WKWebView吧

@available(iOS, introduced: 2.0, deprecated: 12.0, message: "No longer supported; please adopt WKWebView.")
open class UIWebView : UIView, NSCoding, UIScrollViewDelegate { }

到此這篇關于iOS13適配深色模式(Dark Mode)的實現(xiàn)的文章就介紹到這了,更多相關iOS13適配深色模式內容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持創(chuàng)新互聯(lián)!

網(wǎng)站標題:iOS13適配深色模式(DarkMode)的實現(xiàn)
地址分享:http://aaarwkj.com/article24/gghpce.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設計公司移動網(wǎng)站建設、面包屑導航、自適應網(wǎng)站、企業(yè)建站、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿網(wǎng)站建設
人妻中文字幕日韩av| 欧美乱与老熟妇视频观看| 91麻豆视频福利视频| 97在线观看全部视频| 国产福利三级在线观看| 超碰97精品在线观看| 国产免费很黄很色视频| 亚洲av毛片免费在线观看| 国产精品久久乱码综合| 97精品免费视频观看| 国产精品一区二区久久毛片| 国产三级网站在线观看播放| 亚洲小视频免费在线观看| 日韩欧美中文在线一区二区| 一区二区少妇黄色三区| 亚洲成人午夜免费在线观看| 精品一区2区3区4区| 日韩欧美一区二区三区| 欧美久久久久综合一区| 色在色在线播放亚洲中文| 九九热在线免费观看精品视频| 亚洲一区二区精品免费视频| 成熟女人毛茸茸的视频| 日韩人妻一区中文字幕| 啊啊舒服爽用力爱我视频| 国产亚洲精品女人久久久| 国产一区二区主播不卡| 在线免费观看午夜视频| 国产精品国产三级国产普通话99| 日韩高清一级黄色大片网站| 国产91一区二区三区在线精品| 日日夜夜久久一二三区| 国产精品国产三级国产av野外 | 外国男人搞亚洲女人在线| 91精品一久久香蕉国产| 免费观看国产裸体视频| av免费在线不卡观看| 成人黄网站色大片免费观看| 最新亚洲国产高清激情| 久久精品国产亚洲av蜜点| 午夜激情在线观看国产|