需求: 不需要暗黑模式但是不想在info.plist里面全局关闭暗黑模式
- 苹果源码 .preferredColorScheme(.dark)
代码
@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
extension View {
/// Sets the preferred color scheme for this presentation.
///
/// Use one of the values in ``ColorScheme`` with this modifier to set a
/// preferred color scheme for the nearest enclosing presentation, like a
/// popover, a sheet, or a window. The value that you set overrides the
/// user's Dark Mode selection for that presentation. In the example below,
/// the ``Toggle`` controls an `isDarkMode` state variable, which in turn
/// controls the color scheme of the sheet that contains the toggle:
///
/// @State private var isPresented = false
/// @State private var isDarkMode = true
///
/// var body: some View {
/// Button("Show Sheet") {
/// isPresented = true
/// }
/// .sheet(isPresented: $isPresented) {
/// List {
/// Toggle("Dark Mode", isOn: $isDarkMode)
/// }
/// .preferredColorScheme(isDarkMode ? .dark : .light)
/// }
/// }
///
/// If you apply the modifier to any of the views in the sheet --- which in
/// this case are a ``List`` and a ``Toggle`` --- the value that you set
/// propagates up through the view hierarchy to the enclosing
/// presentation, or until another color scheme modifier higher in the
/// hierarchy overrides it. The value you set also flows down to all child
/// views of the enclosing presentation.
///
/// A common use for this modifier is to create side-by-side previews of the
/// same view with light and dark appearances:
///
/// struct MyView_Previews: PreviewProvider {
/// static var previews: some View {
/// MyView().preferredColorScheme(.light)
/// MyView().preferredColorScheme(.dark)
/// }
/// }
///
/// If you need to detect the color scheme that currently applies to a view,
/// read the ``EnvironmentValues/colorScheme`` environment value:
///
/// @Environment(\.colorScheme) private var colorScheme
///
/// var body: some View {
/// Text(colorScheme == .dark ? "Dark" : "Light")
/// }
///
/// - Parameter colorScheme: The preferred color scheme for this view.
///
/// - Returns: A view that sets the color scheme.
@inlinable public func preferredColorScheme(_ colorScheme: ColorScheme?) -> some View
}