天天看點

常用知識之iOS15代碼、功能、屬性新适配

環境工具:iOS15、Xcode13

1、UINavigationBar、UITabBar、UIToolBar

部分屬性在iOS15上是無效的,比如:導航欄顔色、字型顔色、陰影設定等,需要使用iOS13更新的API中UINavigationBarAppearance、UITabBarAppearance的執行個體對象來設定。

對導航欄的性能做了優化,預設情況下,如果導航欄與視圖沒有重疊,導航欄的背景透明,如果重疊,會變成模糊的效果。如果想要一直就是模糊效果,可以通過改變scrollEdgeAppearance屬性來實作。UINavigationBar、UITabBar、UIToolBar将在UIViewController關聯滾動視圖位于适當的邊緣時使用scrollEdgeAppearance屬性。

if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
        barApp.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        self.navigationController.navigationBar.scrollEdgeAppearance = barApp;
        self.navigationController.navigationBar.standardAppearance = barApp;
    }
           

2、UITableView新增一個屬性:sectionHeaderTopPadding,作為清單每個部分标題上方的填充,預設會給每個sectionheader增加一個高度:22px,當使用UITableViewStylePlain初始化UITableView時,會發現這個現象。因為預設屬性是UITableViewAutomaticDimension。可以手動去除此高度。

if (@available(iOS 15.0, *)) {
    table.sectionHeaderTopPadding = 0;
}
           

3、UIImageWriteToSavedPhotosAlbum存儲圖檔之後的回調不再傳回圖檔了,會傳回nil,如果在回調方法裡面操作image有可能會直接Crash,目前的解決辦法聲明一個全局image去記錄,再去操作。

self.image = image;
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
            
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    // self.image doing...
}
           

4、UIButton.configuration是一個新的結構體,它可以配置按鈕的屬性和行為,比如:title、image、buttonSize、cornerStyle、imagePadding、titlePadding等。

5、Json解析支援Json5。

6、UICollectionViewCell、UITableViewCell支援UICellConfigurationState狀态變化時的Block執行。

7、UICollectionViewLayout支援自動高度:automaticDimension。

8、系統圖檔支援多個層,支援多種渲染模式。

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

10、UILabel顯示的文字比設定Font大,在iOS 15中,UILabel 設定adjustsFontSizeToFitWidth為true時,高度不能跟設定的 Font 一樣大。固定寬度的UILabel文字顯示不全,在iOS 15以下計算的正好顯示完UILabel文字的寬度,在iOS 15上可能顯示不全,需要增加寬度。

繼續閱讀