美文网首页
Xcode13.0 与 iOS15.0适配问题

Xcode13.0 与 iOS15.0适配问题

作者: 倒着游的鱼 | 来源:发表于2021-12-02 20:11 被阅读0次

UINavigationBar

if (@available(iOS 15.0, *)) {
    UINavigationBarAppearance * appearance = [[UINavigationBarAppearance alloc] init];
    // 背景色
    appearance.backgroundColor = [UIColor whiteColor];
    // 去除导航栏阴影(如果不设置clear,导航栏底下会有一条阴影线)
    appearance.shadowColor = [UIColor clearColor];
    // 字体颜色
    appearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor redColor]};
    // 带scroll滑动的页面
    self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    // 常规页面
    self.navigationController.navigationBar.standardAppearance = appearance;
    self.navigationController.navigationBar.compactAppearance = appearance;
    self.navigationController.navigationBar.compactScrollEdgeAppearance = appearance;

}

附上项目实例使用

-(void)setTabbarApperence {
    //Set tabBar style.
    UIColor *normalTitleColor = [UIColor colorWithHexString:@"#1E1E1E"];
    UIColor *selectedTitleColor = [UIColor colorWithHexString:@"#02C160"];
    
    //设置tabBar文字颜色
    if (@available(iOS 13.0, *)) {
        // iOS 13以上 item未选中状态下的设置的颜色无效 使用 UITabBarItemAppearance设置
//        self.tabBar.unselectedItemTintColor = [UIColor colorWithHexString:@"#1E1E1E"];
//        self.tabBar.tintColor =  [UIColor colorWithHexString:@"#02C160"];
     
        UITabBarItem *item = [UITabBarItem appearance];
        [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:9]} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:9]} forState:UIControlStateSelected];


    } else {
        // iOS 13以下
        UITabBarItem *item = [UITabBarItem appearance];
        [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:9], NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#1E1E1E"]} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:9], NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#02C160"]} forState:UIControlStateSelected];
        //设置文字与图片距离
        [item setTitlePositionAdjustment:UIOffsetMake(0, -5)];
    }

    //设置tabBar背景色与黑线
    if (@available(iOS 13.0, *)) {

        
        UITabBarItemAppearance *itemAppearance = [[UITabBarItemAppearance alloc] init];
        //设置tabBar文字颜色
        itemAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName : normalTitleColor};
        itemAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName : selectedTitleColor};
        //设置文字与图片距离
        itemAppearance.normal.titlePositionAdjustment = UIOffsetMake(0, -5);
        itemAppearance.selected.titlePositionAdjustment = UIOffsetMake(0, -5);
        
        
        UITabBarAppearance *appearance = [UITabBarAppearance new];
        appearance.backgroundColor = [UIColorFromHex(0XEDEDED) colorWithAlphaComponent:0.9];
        appearance.backgroundImage = [UIImage imageWithColor:[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0]];
        appearance.shadowColor = [UIColor clearColor];
        //设置黑线
        appearance.shadowImage = [UIImage imageWithColor:[UIColor colorWithHexString:@"#D9D9D9"] size:CGSizeMake(SCREEN_WIDTH, 0.5)];

        appearance.stackedLayoutAppearance = itemAppearance;
       

        self.tabBar.standardAppearance = appearance;

        if (@available(iOS 15.0, *)) { //
          self.tabBar.scrollEdgeAppearance = appearance;
        }
    } else {
        // 设置tabbar的线条颜色及阴影
        UITabBar *tabBarAppearance = [UITabBar appearance];

        tabBarAppearance.backgroundColor = [UIColorFromHex(0XEDEDED) colorWithAlphaComponent:0.9];
        tabBarAppearance.backgroundImage = [UIImage imageWithColor:[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0]];
        [tabBarAppearance setShadowImage:[UIImage imageWithColor:[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0] size:CGSizeMake(SCREEN_WIDTH, 0.5)]];

    }


}

UITabBar

    if (@available(iOS 15.0, *)) {
        UITabBarAppearance *barAppearance = [[UITabBarAppearance alloc] init];
        barAppearance.backgroundEffect = nil;
        self.tabBar.scrollEdgeAppearance = barAppearance;
        self.tabBar.standardAppearance = barAppearance;
    } else {
        // Fallback on earlier versions
        [UITabBar appearance].translucent = NO;
    }

iOS14的问题
首次进入可成功设置tabBar未选中状态下的文字颜色,可当点击tabBar上某个item之后该item未选中状态下的设置的颜色无效。
解决方法

//也无效 [[UITabBar appearance] setUnselectedItemTintColor:UIColorHex(#1E1E1E)];
 //Set tabBar style.
    UIColor *normalTitleColor = RGBA(80, 80, 81, 1);
    UIColor *selectedTitleColor = RGBA(42, 109, 240, 1);
    if (@available(iOS 13.0, *)) {
        UITabBarItemAppearance *itemAppearance = [[UITabBarItemAppearance alloc] init];
        itemAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName : normalTitleColor};
        itemAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName : selectedTitleColor};
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        appearance.stackedLayoutAppearance = itemAppearance;
        self.tabBar.standardAppearance = appearance;
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = appearance;
        } else {
            // Fallback on earlier versions
        }
    }else if (@available(iOS 10.0, *)) {
        self.tabBar.tintColor = normalTitleColor;
        self.tabBar.unselectedItemTintColor = selectedTitleColor;
    }else {
        // Fallback on earlier versions
    }

TableView

iOS15 tableView设置sectionHeader高度不生效。

iOS15 创建UITableView为UITableViewStylePlain类型时,设置sectionHeader高度不生效。

iOS15新增属性:sectionHeaderTopPadding,设置为0即可:

if (@available(iOS 15.0, *)) {
    self.tableView.sectionHeaderTopPadding = 0;
} else {
    // Fallback on earlier versions
}

或者在AppDelegate全局设置:

if (@available(iOS 15.0, *)) {
     [UITableView appearance].sectionHeaderTopPadding = 0;
}

navigationBar
导航栏出现下拉透明问题

if (@available(iOS 13.0, *)) {
        
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        
        [appearance configureWithOpaqueBackground];

        appearance.backgroundColor = [UIColor colorWithRed:(34/255.0) green:(34/255.0)  blue:(34/255.0) alpha:1.0];

        self.navigationBar.standardAppearance = appearance;

        self.navigationBar.scrollEdgeAppearance = self.navigationBar.standardAppearance;
        
    } else {
        // Fallback on earlier versions
    }

使用到的类别


@interface UIImage (Extension)

//给图片着色
- (UIImage *)imageToColor:(UIColor *)color;

//通过颜色生成图片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;
@end


@implementation UIImage (Extension)

- (UIImage *)imageToColor:(UIColor *)color {
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, self.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextClipToMask(context, rect, self.CGImage);
    [color setFill];
    CGContextFillRect(context, rect);
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    UIGraphicsBeginImageContextWithOptions(size, NO, .0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    [color set];
    CGContextFillRect(context, CGRectMake(.0, .0, size.width, size.height));
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
@end

//颜色宏
#define UIColorFromHexA(hexValue, a)     [UIColor colorWithRed:(((hexValue & 0xFF0000) >> 16))/255.0f green:(((hexValue & 0xFF00) >> 8))/255.0f blue:((hexValue & 0xFF))/255.0f alpha:a]

#define UIColorFromHex(hexValue)        UIColorFromHexA(hexValue, 1.0f)

相关文章

网友评论

      本文标题:Xcode13.0 与 iOS15.0适配问题

      本文链接:https://www.haomeiwen.com/subject/tpruxrtx.html