iOS13 暗黑模式(浅色 & 深色)

iOS13出来后有个暗黑模式,需要适配,调研了一番,终于解决了,废话不说,先看是如何搞定的。

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (@available(iOS 13.0, *)) {
        self.view.backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if ( traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight ) {    //浅色
                return [UIColor redColor];
            }
            else if ( traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ) {  // 深色
                return [UIColor blueColor];
            }
            else {
                return [UIColor yellowColor];
            }
        }];
    } else {
          
    }
}

这里发现有个UITraitCollection类,之前没有研究过这个类,就跳转到API进去看看。这里大致总结一哈

userInterfaceIdiom:判断当前设备是 iPhone/iPad/tv/carPlay 的哪种

@property (nonatomic, readonly) UIUserInterfaceIdiom userInterfaceIdiom; // unspecified: UIUserInterfaceIdiomUnspecified

typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {
    UIUserInterfaceIdiomUnspecified = -1,
    UIUserInterfaceIdiomPhone API_AVAILABLE(ios(3.2)), // iPhone and iPod touch style UI
    UIUserInterfaceIdiomPad API_AVAILABLE(ios(3.2)), // iPad style UI
    UIUserInterfaceIdiomTV API_AVAILABLE(ios(9.0)), // Apple TV style UI
    UIUserInterfaceIdiomCarPlay API_AVAILABLE(ios(9.0)), // CarPlay style UI
};

userInterfaceStyle:显示也亮度中外观的配置信息

@property (nonatomic, readonly) UIUserInterfaceStyle userInterfaceStyle API_AVAILABLE(tvos(10.0)) API_AVAILABLE(ios(12.0)) API_UNAVAILABLE(watchos); // unspecified: UIUserInterfaceStyleUnspecified

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

layoutDirection:布局方向的配置信息

@property (nonatomic, readonly) UITraitEnvironmentLayoutDirection layoutDirection API_AVAILABLE(ios(10.0)); // unspecified: UITraitEnvironmentLayoutDirectionUnspecified

typedef NS_ENUM(NSInteger, UITraitEnvironmentLayoutDirection) {
    UITraitEnvironmentLayoutDirectionUnspecified = -1,
    UITraitEnvironmentLayoutDirectionLeftToRight = UIUserInterfaceLayoutDirectionLeftToRight,
    UITraitEnvironmentLayoutDirectionRightToLeft = UIUserInterfaceLayoutDirectionRightToLeft,
} API_AVAILABLE(ios(10.0));

displayScale:图片scale的配置信息

@property (nonatomic, readonly) CGFloat displayScale; // unspecified: 0.0

horizontalSizeClass:布局Size Class的配置

@property (nonatomic, readonly) UIUserInterfaceSizeClass horizontalSizeClass; // unspecified:

@property (nonatomic, readonly) UIUserInterfaceSizeClass verticalSizeClass; // unspecified: UIUserInterfaceSizeClassUnspecified

typedef NS_ENUM(NSInteger, UIUserInterfaceSizeClass) {
    UIUserInterfaceSizeClassUnspecified = 0,
    UIUserInterfaceSizeClassCompact     = 1,
    UIUserInterfaceSizeClassRegular     = 2,
} API_AVAILABLE(ios(8.0));

forceTouchCapability:forceTouch是否可用的配置

@property (nonatomic, readonly) UIForceTouchCapability forceTouchCapability API_AVAILABLE(ios(9.0)); // unspecified: UIForceTouchCapabilityUnknown

typedef NS_ENUM(NSInteger, UIForceTouchCapability) {
    UIForceTouchCapabilityUnknown = 0,
    UIForceTouchCapabilityUnavailable = 1,
    UIForceTouchCapabilityAvailable = 2
};

preferredContentSizeCategory:全局字体大小的配置

@property (nonatomic, copy, readonly) UIContentSizeCategory preferredContentSizeCategory API_AVAILABLE(ios(10.0)); // unspecified: UIContentSizeCategoryUnspecified

typedef NSString * UIContentSizeCategory NS_TYPED_ENUM API_AVAILABLE(ios(7.0));

// Content size category constants

UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryUnspecified API_AVAILABLE(ios(10.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryExtraSmall API_AVAILABLE(ios(7.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategorySmall API_AVAILABLE(ios(7.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryMedium API_AVAILABLE(ios(7.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryLarge API_AVAILABLE(ios(7.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryExtraLarge API_AVAILABLE(ios(7.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryExtraExtraLarge API_AVAILABLE(ios(7.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryExtraExtraExtraLarge API_AVAILABLE(ios(7.0));

// Accessibility sizes
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryAccessibilityMedium API_AVAILABLE(ios(7.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryAccessibilityLarge API_AVAILABLE(ios(7.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryAccessibilityExtraLarge API_AVAILABLE(ios(7.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryAccessibilityExtraExtraLarge API_AVAILABLE(ios(7.0));
UIKIT_EXTERN UIContentSizeCategory const UIContentSizeCategoryAccessibilityExtraExtraExtraLarge API_AVAILABLE(ios(7.0));

displayGamut:色域的配置

@property (nonatomic, readonly) UIDisplayGamut displayGamut API_AVAILABLE(ios(10.0)); // unspecified: UIDisplayGamutUnspecified

typedef NS_ENUM(NSInteger, UIDisplayGamut) {
    UIDisplayGamutUnspecified = -1, // UIKit will not set this anymore, instead a sensible default is chosen based on the device capabilities and settings always
    UIDisplayGamutSRGB,
    UIDisplayGamutP3
} API_AVAILABLE(ios(10.0));

accessibilityContrast:是否开启高对比度的配置

@property (nonatomic, readonly) UIAccessibilityContrast accessibilityContrast API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos); // unspecified: UIAccessibilityContrastUnspecified

typedef NS_ENUM(NSInteger, UIAccessibilityContrast) {
    UIAccessibilityContrastUnspecified = -1,
    UIAccessibilityContrastNormal,
    UIAccessibilityContrastHigh,
} API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);

userInterfaceLevel:指示窗口中内容的可视级别

@property (nonatomic, readonly) UIUserInterfaceLevel userInterfaceLevel API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos) API_UNAVAILABLE(watchos); // unspecified: UIUserInterfaceLevelUnspecified

typedef NS_ENUM(NSInteger, UIUserInterfaceLevel) {
    UIUserInterfaceLevelUnspecified = -1,
    UIUserInterfaceLevelBase,
    UIUserInterfaceLevelElevated
} API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos) API_UNAVAILABLE(watchos);

legibilityWeight:全局字重的配置

@property (nonatomic, readonly) UILegibilityWeight legibilityWeight API_AVAILABLE(ios(13.0), tvos(13.0), watchos(6.0)); // unspecified: UILegibilityWeightUnspecified

typedef NS_ENUM(NSInteger, UILegibilityWeight) {
    UILegibilityWeightUnspecified = -1,
    UILegibilityWeightRegular,
    UILegibilityWeightBold
} API_AVAILABLE(ios(13.0), tvos(13.0), watchos(6.0));

如何获取全局的UITraitCollection

[UITraitCollection currentTraitCollection];

你可能感兴趣的:(iOS13 暗黑模式(浅色 & 深色))