iOS15新特性適配(告警:UITableView sectionHeader下移22像素,間距變高)

1、UINavigationBar

在iOS 15中,UINavigationBar默認(rèn)為透明。在滑動(dòng)時(shí)會(huì)有模糊效果。如果想要一直就是模糊效果,可以通過(guò)改變scrollEdgeAppearance屬性來(lái)實(shí)現(xiàn)。

解決辦法:

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

barApp.backgroundEffect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];

self.navigationBar.scrollEdgeAppearance=barApp;

UINavigationBar、UIToolbar 和 UITabBar 將在你的VC關(guān)聯(lián)滾動(dòng)視圖位于適當(dāng)?shù)倪吘墪r(shí)使用 scrollEdgeAppearance(或者如果您的視圖層級(jí)結(jié)構(gòu)中沒(méi)有 UIScrollView,更多內(nèi)容見(jiàn)下文)。

您必須使用 UIBarAppearance API 來(lái)自定義。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

這里我看到好多文章說(shuō)和navigationbar是一個(gè)毛病,背景色和圖片設(shè)置在iOS 15上面都不會(huì)生效,但是我經(jīng)過(guò)測(cè)試,oc是沒(méi)有毛病的!swift語(yǔ)言確實(shí)會(huì)不生效。為了統(tǒng)一做了如下改動(dòng)

代碼和效果圖如下:

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 新增了一個(gè)屬性:sectionHeaderTopPadding。此屬性會(huì)給每一個(gè) section header 增加一個(gè)默認(rèn)高度,當(dāng)我們使用 UITableViewStylePlain 初始化UITableView 的時(shí)候,系統(tǒng)默認(rèn)給 section header 增高了22像素。

解決辦法:

object-c / swift:

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

3、對(duì)狀態(tài)編程的支持:UICellConfigurationState;UICollectionViewCell、UITableViewCell都支持狀態(tài)變化時(shí)的block執(zhí)行了。

4、UICollectionViewLayout支持自動(dòng)高度;AutomaticDimension

5、json解析支持json5了

6、增加UISheetPresentationController,通過(guò)它可以控制 Modal 出來(lái)的 UIViewController 的顯示大小,且可以通過(guò)拖拽手勢(shì)在不同大小之間進(jìn)行切換。

7、UIButton支持更多配置。UIButton.Configuration是一個(gè)新的結(jié)構(gòu)體,它指定按鈕及其內(nèi)容的外觀和行為。它有許多與按鈕外觀和內(nèi)容相關(guān)的屬性,如cornerStyle、baseForegroundColor、baseBackgroundColor、buttonSize、title、image、subtitle、titlePadding、imagePadding、contentInsets、imagePlacement等。

image.png

之前我們可能要自定義一個(gè)類來(lái)實(shí)現(xiàn)這種按鈕,但是現(xiàn)在蘋果新增了特性

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:@"大標(biāo)題"];

[conf setSubtitle:@"副標(biāo)題"];

[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 = "大標(biāo)題"

conf.subtitle = "副標(biāo)題"

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

conf.imagePlacement = .leading

button.configuration = conf

}else{

// Fallback on earlier versions

}

8、推出CLLocationButton用于一次性定位授權(quán),該內(nèi)容內(nèi)置于CoreLocationUI模塊,但如果需要獲取定位的詳細(xì)信息仍然需要借助于CoreLocation。

9、URLSession 推出支持 async/await 的 API,包括獲取數(shù)據(jù)、上傳與下載

10、系統(tǒng)圖片支持多個(gè)層,支持多種渲染模式。

11、UIImage 新增了幾個(gè)調(diào)整尺寸的方法。

圖片的尺寸變換

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))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • UINavigationBar 在iOS 15中,UINavigationBar默認(rèn)為透明。在滑動(dòng)時(shí)會(huì)有模糊效果。...
    Summit_yp閱讀 20,091評(píng)論 7 54
  • iOS 11適配源碼 Demo地址 安全區(qū)域的適配 用Xcode 9 創(chuàng)建storyboard或者xib時(shí),最低版...
    Bytesking閱讀 6,728評(píng)論 5 24
  • 前言 iOS 15在2021 WWDC會(huì)后發(fā)布,就勇猛的把水果全家桶都升級(jí)了最新系統(tǒng)。兩個(gè)iOS 15 beta版...
    TripleEyeAline閱讀 7,628評(píng)論 2 11
  • 開(kāi)發(fā)遇到親身體驗(yàn),總結(jié)此文共享與廣大開(kāi)發(fā)者,希望你更快解決bug。ios11變化做大的要輸導(dǎo)航欄 一. UITab...
    蓑衣微涼閱讀 434評(píng)論 0 0
  • iOS7到現(xiàn)在已經(jīng)發(fā)布了有一段時(shí)間了。相信你現(xiàn)在已經(jīng)了解了它那些開(kāi)創(chuàng)性的視覺(jué)設(shè)計(jì),已經(jīng)了解了它的新的API,比如說(shuō)...
    全力以赴打醬油閱讀 1,138評(píng)論 1 0

友情鏈接更多精彩內(nèi)容