iOS13之后处理方法稍有变更,先记录一下,希望能对遇到同样问题的coder有所帮助。
1、去除tabBar上的黑线
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [self.tabBar.standardAppearance copy];
appearance.backgroundImage = [UIImage imageNamed:@"tabbar_bg"];
appearance.shadowImage = [UIImage new];
appearance.shadowColor = [UIColor clearColor];
self.tabBar.standardAppearance = appearance;
} else {
[self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar_bg"]];
[self.tabBar setShadowImage:[UIImage new]];
}
如果你只是简单的想去除上面的黑条,而又没有控件超出tabbar,可以直接用下面的方法:
self.tabBar.clipsToBounds = YES;
2、自定义各个tabBarItem:iOS13后不再支持对各个Item样式的单独定制
// TabBarViewController
- (void)viewDidLoad {
UITabBarAppearance *appearance = self.tabBar.standardAppearance;
[appearance.stackedLayoutAppearance.normal setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexString:@"AEBBC2"],NSFontAttributeName:[UIFont systemFontOfSize:10.0f]}];
self.tabBar.standardAppearance = appearance;
}
// 赋值各个Item选中时的个性化设置
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (@available(iOS 13, *)) {
UITabBarAppearance *appearance = self.tabBar.standardAppearance;
[appearance.stackedLayoutAppearance.selected setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexString:self.titleColors[tabBarController.selectedIndex]],NSFontAttributeName:[UIFont systemFontOfSize:10.0f]}];
self.tabBar.standardAppearance = appearance;
}
}
效果图
3、禁止跟随系统的暗黑模式
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDarkContent</string>
在appDelegate
里,
- (void)configLightMode {
if (@available(iOS 13.0, *)) {
self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
}
指定页面的statusBarStyle变换,请参考iOS:状态栏
4、TabBar透明
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [self.tabBar.standardAppearance copy];
appearance.backgroundImage = [UIImage new];
appearance.shadowImage = [UIImage new];
appearance.shadowColor = [UIColor clearColor];
appearance.backgroundEffect = nil;
self.tabBar.standardAppearance = appearance;
} else {
[self.tabBar setBarTintColor:[UIColor clearColor]];
[self.tabBar setBackgroundImage:[UIImage new]];
}
5、适配暗黑模式
a. 颜色适配
// MZInterfaceStyleService.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MZInterfaceStyleService : NSObject
/// 手动控制适配模式(全局的) 默认为开启暗黑模式
@property (nonatomic, assign, class) BOOL openDarkMode;
@property (nonatomic, assign, class, readonly, getter=isCurrentModeDark) BOOL currentModeDark;
/// label文字颜色 如果有多种文字颜色可以设置多个 eg: labelTextColor
@property (nonatomic, strong, class, readonly) UIColor *labelTextColor;
/// button文字颜色 如果有多种文字颜色可以设置多个 eg: buttonTextColor
@property (nonatomic, strong, class, readonly) UIColor *buttonTextColor;
/// textField文字颜色 如果有多种文字颜色可以设置多个 eg: fieldTextColor
@property (nonatomic, strong, class, readonly) UIColor *fieldTextColor;
/// label背景颜色 如果有多种文字颜色可以设置多个 eg: labelBgColor
@property (nonatomic, strong, class, readonly) UIColor *labelBgColor;
/// button背景颜色 如果有多种文字颜色可以设置多个 eg: buttonBgColor
@property (nonatomic, strong, class, readonly) UIColor *buttonBgColor;
/// view背景颜色 如果有多种文字颜色可以设置多个 eg: viewBgColor
@property (nonatomic, strong, class, readonly) UIColor *viewBgColor;
@end
NS_ASSUME_NONNULL_END
// MZInterfaceStyleService.m
#import "MZInterfaceStyleService.h"
static BOOL _isOpenDarkMode = YES;
@implementation MZInterfaceStyleService
/// 以下的颜色,都需要根据自己项目的实际需要进行定制
+ (UIColor *)labelTextColor {
return [self getCurrentModeColorWithDark:UIColor.whiteColor light:UIColor.redColor];
}
+ (UIColor *)buttonTextColor {
return [self getCurrentModeColorWithDark:UIColor.whiteColor light:UIColor.redColor];
}
+ (UIColor *)fieldTextColor {
return [self getCurrentModeColorWithDark:UIColor.whiteColor light:UIColor.redColor];
}
+ (UIColor *)labelBgColor {
return [self getCurrentModeColorWithDark:UIColor.whiteColor light:UIColor.redColor];
}
+ (UIColor *)buttonBgColor {
return [self getCurrentModeColorWithDark:UIColor.whiteColor light:UIColor.redColor];
}
+ (UIColor *)viewBgColor {
return [self getCurrentModeColorWithDark:UIColor.whiteColor light:UIColor.redColor];
}
// 根据当前模式,获得动态颜色
+ (UIColor *)getCurrentModeColorWithDark:(UIColor *)dark light:(UIColor *)light {
// 如果不需要打开暗黑模式,直接返回light
if (!MZInterfaceStyleService.openDarkMode) {
return light;
}
if (@available(iOS 13.0, *)) {
return [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
return traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ? dark : light;
}];
}
return light;
}
#pragma mark - 是否启用暗黑模式
+ (BOOL)openDarkMode {
return _isOpenDarkMode;
}
+ (void)setOpenDarkMode:(BOOL)openDarkMode {
_isOpenDarkMode = openDarkMode;
}
+ (BOOL)isCurrentModeDark {
if (!_isOpenDarkMode) {
return NO;
}
if (@available(iOS 13.0, *)) {
return UITraitCollection.currentTraitCollection.userInterfaceStyle == UIUserInterfaceStyleDark;
}
return NO;
}
@end
使用
[self.clickBtn setTitleColor:MZInterfaceStyleService.buttonTextColor forState:UIControlStateNormal];
b. 图片适配
在 Images.xcassets 中设置颜色和图片是向下兼容的,iOS 13 以下的系统会默认取 Any
状态下的设定,iOS 13 会根据系统模式取 Any
、Light
或 Dark
下的设定。
补充
在 iOS 13中,UIView、UIViewController 、UIWindow 有了一个 overrideUserInterfaceStyle
的新属性,可以覆盖系统的模式。单个页面或视图关闭暗黑模式,设置 overrideUserInterfaceStyle
为对应的模式,强制限制该视图与其子视图以设置的模式进行展示,不跟随系统模式改变进行改变。
如:
self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
设置此属性会影响当前 view/viewController/window 以及它下面的任何内容。
如果你希望一个子视图监听系统的模式,请将 overrideUserInterfaceStyle
属性设置为UIUserInterfaceStyleUnspecified
。
该部分图片适配参考:
iOS 13 DarkMode 暗黑模式
网友评论