iOS15适配踩坑之旅

前言

目前是基于XCode13 beta, iOS15.说不定苹果良心发现,后面自动好了.

NavigationBar颜色及背景失效

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];
}

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
复制代码

🤔 上面是demo代码,意图就是给导航栏设置一个背景图片,图片由颜色生成 (业务场景需要,你可以直接设置barTintColor或者正常的背景图片)

😭 然而,好好的代码在XCode13 beta,iOS 15下直接gg

UINavigationBarAppearance *app = [UINavigationBarAppearance new];
[app configureWithOpaqueAppearance];
app.backgroundColor = UIColor.redColor;
navigationBar.scrollEdgeAppearance = app
navigationBar.standardAppearance = app;
复制代码

后来查找无果, 就去苹果论坛反馈了下,然后得到了上面的反馈.

详细内容可以见 developer.apple.com/forums/thre…

😄 简单来说,就是用iOS13 新出来的API.然后老老实实设置apperance,个人感觉还是有bug的,我测试的时候必须要设置scrollEdgeAppearance才有效果.按照苹果官方文档来说,如果scrollEdgeAppearance为nil,会自动用standardAppearance.奈何我就是个小开发...

tableview header section设置为0失效

self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *label = [[UILabel alloc] init];
    label.text = @"test";
    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 0.01;

}
复制代码

🤬 代码大致是这样,也是个demo. 0.01随便怎么改都没有卵用.

后面去看了下API改动

发现了一个这玩意儿

var sectionHeaderTopPadding: CGFloat { get set }
复制代码

然后直接

if (@available(iOS 15.0, *)) {
    _tableView.sectionHeaderTopPadding = 0;
}
复制代码

😂 世界都清净了,但个人感觉后续更定会修的,这玩意儿也太坑了...

总结

👨🏻‍💻 目前来说,踩到这两个坑.后续有新的会继续发出来.也欢迎大佬们留言讨论下新的坑. 然后其中也有两个不错的方法: 1. 苹果论坛发帖, 我第一个问题就是苹果员工回复的 2. 看下API更新文档, 更新部分都会特别注明.

分类:
iOS
标签: