iOS | iOS13中Tabbar黑线隐藏与NavigationBar透明的适配

以前好好的透明方案放到13走不通了,只能摸索新方案适配了,☹️


Tabbar

先说Tabbar黑线的问题,在iOS13中,出现新的属性控制Bar

/// Describes the appearance attributes for the tab bar to use.
@property (nonatomic, readwrite, copy) UITabBarAppearance *standardAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(13.0), tvos(13.0));

琢磨了一下,得适配阿,整整

if (@available(iOS 13.0, *)) {
    UITabBarAppearance *standardAppearance = [self.tabBar.standardAppearance copy];
    standardAppearance.shadowImage = [UIImage new];
    standardAppearance.shadowColor = kClearColor;
    self.tabBar.standardAppearance = standardAppearance;
} else {
    [self.tabBar setShadowImage:[UIImage new]];
}

NavigationBar

再说NavBar透明问题,同样的对于Bar,iOS13增加新控制属性

/// Describes the appearance attributes for the navigation bar to use when it is displayed with its standard height.
@property (nonatomic, readwrite, copy) UINavigationBarAppearance *standardAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(13.0), tvos(13.0));
/// Describes the appearance attributes for the navigation bar to use when it is displayed with its compact height. If not set, the standardAppearance will be used instead.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *compactAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(13.0));
/// Describes the appearance attributes for the navigation bar to use when an associated UIScrollView has reached the edge abutting the bar (the top edge for the navigation bar). If not set, a modified standardAppearance will be used instead.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *scrollEdgeAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(13.0));

取咱们需要的standardAppearance做文章

/// 是否透明
BOOL alpha;

// 标题属性
NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName:{Your Color}, 
                                                 NSFontAttributeName:{Your Font}};

if (@available(iOS 13.0, *)) {
       UINavigationBarAppearance *standardAppearance = [self.navigationBar.standardAppearance copy];
       standardAppearance.titleTextAttributes = titleTextAttributes;
       //设置此参数将覆盖`BarTintColor`
       standardAppearance.backgroundColor = {Your Color};
       //透明
       standardAppearance.backgroundImage = alpha?[UIImage new]:nil;
       standardAppearance.backgroundEffect = nil;
       self.navigationBar.standardAppearance = standardAppearance;
} else {
       //标题颜色
       self.navigationBar.titleTextAttributes = titleTextAttributes;
       //背景色
       [self.navigationBar setBarTintColor:{Your Color}];
       //透明(key code)
       [self.navigationBar setBackgroundImage:alpha?[UIImage new]:nil forBarMetrics:UIBarMetricsDefault];
}

一开始设置完backgroundImage寻思着应该ok,结果事与愿违,发现UIVisualEffectView还在生效,怪咧,明明我有设置

[self.navigationBar setTranslucent:FALSE];

‍♀️,进@interface UIBarAppearance里找关键代码

/// A specific blur effect to use for the bar background. This effect is composited first when constructing the bar's background.
@property (nonatomic, readwrite, copy, nullable) UIBlurEffect *backgroundEffect;

就是你,整蛊作怪,一看nullable,当下我决定消灭你

standardAppearance.backgroundEffect = nil;

去掉黑线与Tarbar类似
/// 不用上BarAppearance做透明的话,直接setShadowImage可行
if (@available(iOS 13.0, *)) {
     UINavigationBarAppearance *standardAppearance = [self.navigationBar.standardAppearance copy];
     standardAppearance.shadowImage = [UIImage new];
     standardAppearance.shadowColor = kClearColor;
     self.navigationBar.standardAppearance = standardAppearance;
}
else {
     [self.navigationBar setShadowImage:[UIImage new]];
}

按理儿Tarbar透明也是差不多整法,有需求可以按这个路子试试,我这边收工

你可能感兴趣的:(iOS | iOS13中Tabbar黑线隐藏与NavigationBar透明的适配)