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

1甜刻、UINavigationBar

在iOS 15中绍撞,UINavigationBar默認為透明。在滑動時會有模糊效果罢吃。如果想要一直就是模糊效果楚午,可以通過改變scrollEdgeAppearance屬性來實現(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)?shù)倪吘墪r使用 scrollEdgeAppearance(或者如果您的視圖層級結(jié)構(gòu)中沒有 UIScrollView,更多內(nèi)容見下文)阱驾。

您必須使用 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是一個毛病里覆,背景色和圖片設(shè)置在iOS 15上面都不會生效丧荐,但是我經(jīng)過測試,oc是沒有毛病的喧枷!swift語言確實會不生效虹统。為了統(tǒ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 新增了一個屬性:sectionHeaderTopPadding隧甚。此屬性會給每一個 section header 增加一個默認高度车荔,當(dāng)我們使用 UITableViewStylePlain 初始化UITableView 的時候,系統(tǒng)默認給 section header 增高了22像素戚扳。

解決辦法:

object-c / swift:

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

3忧便、對狀態(tài)編程的支持:UICellConfigurationState;UICollectionViewCell帽借、UITableViewCell都支持狀態(tài)變化時的block執(zhí)行了珠增。

4、UICollectionViewLayout支持自動高度砍艾;AutomaticDimension

5蒂教、json解析支持json5了

6、增加UISheetPresentationController脆荷,通過它可以控制 Modal 出來的 UIViewController 的顯示大小凝垛,且可以通過拖拽手勢在不同大小之間進行切換。

7、UIButton支持更多配置苔严。UIButton.Configuration是一個新的結(jié)構(gòu)體定枷,它指定按鈕及其內(nèi)容的外觀和行為。它有許多與按鈕外觀和內(nèi)容相關(guān)的屬性届氢,如cornerStyle欠窒、baseForegroundColor、baseBackgroundColor退子、buttonSize岖妄、title、image寂祥、subtitle荐虐、titlePadding、imagePadding丸凭、contentInsets福扬、imagePlacement等。

image.png

之前我們可能要自定義一個類來實現(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模塊虽界,但如果需要獲取定位的詳細信息仍然需要借助于CoreLocation汽烦。

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

10、系統(tǒng)圖片支持多個層礁叔,支持多種渲染模式牍颈。

11、UIImage 新增了幾個調(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)容合作請聯(lián)系作者
  • 序言:七十年代末颂砸,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子死姚,更是在濱河造成了極大的恐慌人乓,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件都毒,死亡現(xiàn)場離奇詭異色罚,居然都是意外死亡,警方通過查閱死者的電腦和手機账劲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門戳护,熙熙樓的掌柜王于貴愁眉苦臉地迎上來金抡,“玉大人,你說我怎么就攤上這事腌且」8危” “怎么了?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵铺董,是天一觀的道長巫击。 經(jīng)常有香客問我,道長精续,這世上最難降的妖魔是什么坝锰? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮重付,結(jié)果婚禮上顷级,老公的妹妹穿的比我還像新娘。我一直安慰自己确垫,他們只是感情好弓颈,可當(dāng)我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著森爽,像睡著了一般恨豁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上爬迟,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天,我揣著相機與錄音菊匿,去河邊找鬼付呕。 笑死,一個胖子當(dāng)著我的面吹牛跌捆,可吹牛的內(nèi)容都是我干的徽职。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼佩厚,長吁一口氣:“原來是場噩夢啊……” “哼姆钉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起抄瓦,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤潮瓶,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后钙姊,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體毯辅,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年煞额,在試婚紗的時候發(fā)現(xiàn)自己被綠了思恐。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沾谜。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖胀莹,靈堂內(nèi)的尸體忽然破棺而出基跑,到底是詐尸還是另有隱情,我是刑警寧澤描焰,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布涩僻,位于F島的核電站,受9級特大地震影響栈顷,放射性物質(zhì)發(fā)生泄漏逆日。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一萄凤、第九天 我趴在偏房一處隱蔽的房頂上張望室抽。 院中可真熱鬧,春花似錦靡努、人聲如沸坪圾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽兽泄。三九已至,卻和暖如春漾月,著一層夾襖步出監(jiān)牢的瞬間病梢,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工梁肿, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蜓陌,地道東北人。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓吩蔑,卻偏偏與公主長得像钮热,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子烛芬,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容