首先iOS13更新了新的类 UIBarAppearance 来设置系统或者自定义的Tabbar等控件的属性:拿 UITabbar来讲,新增了standardAppearance的属性,standardAppearance属于UITabBarAppearance类,UITabBarAppearance继承自UIBarAppearance。我们如果在iOS13系统环境下想要设置Tabbar的一些属性,比如分割线颜色,就需要用下面的方法来设置:
// 修改tabbar顶部分割线的颜色
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [UITabBarAppearance new];
appearance.shadowColor = kCOLOR_HEX(0xEFEFEF);
appearance.backgroundColor = kCOLOR_HEX(0xFFFFFF);
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = NSTextAlignmentCenter;
UITabBarItemStateAppearance *normalAppearance = appearance.stackedLayoutAppearance.normal;
// 修改文字到图片的距离
normalAppearance.titlePositionAdjustment = UIOffsetMake(0, -15);
UITabBarItemStateAppearance *selectedAppearance = appearance.stackedLayoutAppearance.selected; selectedAppearance.titlePositionAdjustment = UIOffsetMake(0, -15);
self.tabBar.standardAppearance = appearance;
} else {
// Fallback on earlier versions
}
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [UITabBarAppearance new];
} else { // Fallback on earlier versions }
需要注意的是,iOS13只有使用appearance属性才能有效显示,使用之前的老方法就无法显示,所以为了更好的适配不同系统版本,还需要添加之前的方法:
[self.tabBar setBackgroundImage:XXX];
[self.tabBar setShadowImage:XXX];
文章的目的只是做个提醒,方法没写全~
网友评论