iOS15新特性适配(告警:UITableView sectionHeader下移22像素,间距变高)

1、UINavigationBar

在iOS 15中,UINavigationBar默认为透明。在滑动时会有模糊效果。如果想要一直就是模糊效果,可以通过改变scrollEdgeAppearance属性来实现。

解决办法:

UINavigationBarAppearance*barApp=[[UINavigationBarAppearance alloc]init];

barApp.backgroundEffect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];

self.navigationBar.scrollEdgeAppearance=barApp;

UINavigationBar、UIToolbar 和 UITabBar 将在你的VC关联滚动视图位于适当的边缘时使用 scrollEdgeAppearance(或者如果您的视图层级结构中没有 UIScrollView,更多内容见下文)。

您必须使用 UIBarAppearance API 来自定义。UIToolbar 和 UITabBar 为此在 iOS 15 中添加了 scrollEdgeAppearance 属性。

解决办法:

object-c:

if(@available(iOS 15.0,*)){

UINavigationBarAppearance*barApp=[UINavigationBarAppearance new];

barApp.backgroundColor=[[UIColor blueColor]colorWithAlphaComponent:0.5];

//图片

// navBarApperance.backgroundImage = [UIImage imageNamed:@"bgImage.png"];

// navBarApperance.backgroundImageContentMode = UIViewContentModeScaleToFill;


NSDictionary *dictM = @{ NSForegroundColorAttributeName:[UIColor blackColor]};

barApp.titleTextAttributes = dictM;

self.navigationController.navigationBar.scrollEdgeAppearance=barApp;

self.navigationController.navigationBar.standardAppearance=barApp;

}

swift:

if #available(iOS 15.0,*) {

let navBarAppecrace = UINavigationBarAppearance()

//颜色// navBarAppecrace.backgroundColor = UIColor.red

//图片 navBarAppecrace.backgroundImage = UIImage.init(named: "bgImage.png")

navBarAppecrace.backgroundImageContentMode = .scaleToFill

navBarAppecrace.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]

self.navigationController?.navigationBar.standardAppearance = navBarAppecrace

self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppecrace

} else {

self.navigationController?.navigationBar.barTintColor = UIColor.red

// self.navigationController?.navigationBar.setBackgroundImage(UIImage.init(named: "bgImage.png"), for: .any, barMetrics: .default)

}


1.1:TabBar

这里我看到好多文章说和navigationbar是一个毛病,背景色和图片设置在iOS 15上面都不会生效,但是我经过测试,oc是没有毛病的!swift语言确实会不生效。为了统一做了如下改动

代码和效果图如下:

object-c:

if (@available(iOS 15.0, *)) {

UITabBarAppearance *tabBarAppearanc = [UITabBarAppearance new];

//颜色 tabBarAppearanc.backgroundColor = [UIColor greenColor];

// //图片// tabBarAppearanc.backgroundImage = [UIImage imageNamed:@"bgImage.png"];

// tabBarAppearanc.backgroundImageContentMode = UIViewContentModeScaleToFill;

NSDictionary *dictM = @{ NSForegroundColorAttributeName:[UIColor redColor]};

tabBarAppearanc.stackedLayoutAppearance.selected.titleTextAttributes = dictM;

// tabBarAppearanc.stackedLayoutAppearance.normal.titleTextAttributes = .....

self.tabBar.scrollEdgeAppearance = tabBarAppearanc;

self.tabBar.standardAppearance = tabBarAppearanc;

} else {

// Fallback on earlier versions

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];

//颜色 [[UITabBar appearance] setBarTintColor:[UIColor greenColor]];

//图片

// [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"bgImage.png"]];

}

swift:

if #available(iOS 15.0,*) {

let tabBarAppearanc = UITabBarAppearance.init()

//颜色

// tabBarAppearanc.backgroundColor = UIColor.green

//图片 tabBarAppearanc.backgroundImage = UIImage.init(named: "bgImage.png")

tabBarAppearanc.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]

// tabBarAppearanc.stackedLayoutAppearance.normal.titleTextAttributes = .....

self.tabBar.standardAppearance = tabBarAppearanc

self.tabBar.scrollEdgeAppearance = tabBarAppearanc

}else{

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .normal)

//颜色 UITabBar.appearance().barTintColor = UIColor.green

//图片// UITabBar.appearance().backgroundImage = UIImage.init(named: "bgImage.png")

}

2、iOS 15 UITableView sectionHeader下移22像素

iOS 15中 UITableView 新增了一个属性:sectionHeaderTopPadding。此属性会给每一个 section header 增加一个默认高度,当我们使用 UITableViewStylePlain 初始化UITableView 的时候,系统默认给 section header 增高了22像素。

解决办法:

object-c / swift:

if(@available(iOS 15.0,*)){tableView.sectionHeaderTopPadding=0;}     //去掉headerpadding的高度

3、对状态编程的支持:UICellConfigurationState;UICollectionViewCell、UITableViewCell都支持状态变化时的block执行了。

4、UICollectionViewLayout支持自动高度;AutomaticDimension

5、json解析支持json5了

6、增加UISheetPresentationController,通过它可以控制 Modal 出来的 UIViewController 的显示大小,且可以通过拖拽手势在不同大小之间进行切换。

7、UIButton支持更多配置。UIButton.Configuration是一个新的结构体,它指定按钮及其内容的外观和行为。它有许多与按钮外观和内容相关的属性,如cornerStyle、baseForegroundColor、baseBackgroundColor、buttonSize、title、image、subtitle、titlePadding、imagePadding、contentInsets、imagePlacement等。

image.png

之前我们可能要自定义一个类来实现这种按钮,但是现在苹果新增了特性

object-c:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.backgroundColor = [UIColor greenColor];

if (@available(iOS 15.0, *)) {

UIButtonConfiguration *conf = [UIButtonConfiguration tintedButtonConfiguration];

conf.cornerStyle = UIButtonConfigurationCornerStyleMedium;

[conf setImagePadding:5];

[conf setTitle:@"大标题"];

[conf setSubtitle:@"副标题"];

[conf setImage:[UIImage imageNamed:@"btnImage.png"]];

conf.imagePlacement = NSDirectionalRectEdgeLeading; button.configuration = conf;

} else {

// Fallback on earlier versions

}

swift:

let button:UIButton = UIButton.init(type: .custom)

button.backgroundColor = UIColor.green

if #available(iOS 15.0, *) {

var conf = UIButton.Configuration.tinted()

conf.cornerStyle = UIButton.Configuration.CornerStyle.medium

conf.imagePadding = 5

conf.title = "大标题"

conf.subtitle = "副标题"

conf.image = UIImage.init(named: "btnImage.png")

conf.imagePlacement = .leading

button.configuration = conf

}else{

// Fallback on earlier versions

}

8、推出CLLocationButton用于一次性定位授权,该内容内置于CoreLocationUI模块,但如果需要获取定位的详细信息仍然需要借助于CoreLocation。

9、URLSession 推出支持 async/await 的 API,包括获取数据、上传与下载

10、系统图片支持多个层,支持多种渲染模式。

11、UIImage 新增了几个调整尺寸的方法。

图片的尺寸变换

object-c:

UIImage *modeImg = [UIImage imageNamed:@"bgImage.png"];

NSLog(@"图片原尺寸%@",NSStringFromCGSize(modeImg.size));

if (@available(iOS 15.0, *)) {

modeImg = [modeImg imageByPreparingThumbnailOfSize:CGSizeMake(220, 100)];

NSLog(@"**111111**变换后图片原尺寸%@",NSStringFromCGSize(modeImg.size));

[modeImg prepareThumbnailOfSize:CGSizeMake(220, 100) completionHandler:^(UIImage * _Nullable) {

NSLog(@"###222###变换后图片原尺寸%@",NSStringFromCGSize(modeImg.size));

}];

} else {

// Fallback on earlier versions

};

//打印值

//2021-12-15 15:48:17.590981+0800 iOS15UI[71637:683127] 图片原尺寸{674, 206}

//2021-12-15 15:48:17.598647+0800 iOS15UI[71637:683127] **111111**变换后图片原尺寸{220, 100}

//2021-12-15 15:48:17.600537+0800 iOS15UI[71637:684293] ###222###变换后图片原尺寸{220, 100}

swift:

var modImg = UIImage.init(named: "btnImage.png")

print("原图片尺寸%@",modImg?.size as Any)

if #available(iOS 15.0, *) {

modImg = modImg?.preparingThumbnail(of: CGSize(width: 220, height: 100))

print("**111111**变换后图片原尺寸%@",modImg?.size as Any)

modImg?.prepareThumbnail(of: CGSize(width: 220, height: 100)){

img in print("###222###变换后图片原尺寸%@",modImg?.size as Any)

}

}

//打印值

//原图片尺寸%@ Optional((124.0, 120.0))

//**111111**变换后图片原尺寸%@ Optional((220.0, 100.0))

//###222###变换后图片原尺寸%@ Optional((220.0, 100.0))

你可能感兴趣的:(iOS15新特性适配(告警:UITableView sectionHeader下移22像素,间距变高))