iOS15適配小結

1、tabbar及navicationbar的背景顏色問題

問題:從ios14升級到ios15會出現 導航欄背景顏色失效

原因:因為設置顏色方法在ios15中失效

–在iOS13更新的API中新增瞭針對navigationBar,tabbar分別新增瞭新的屬性專門管理這些滑動時候產生的顏色透明等等信息,由於我們應用兼容iOS10以上,對於導航欄的設置還沒有使用UINavigationBarAppearance和UITabBarAppearance,但在更新的iOS15上失效,所以就變得設置失效

//設置navigationBar顏色
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
//設置tabBar背景色
self.tabBarController.tabBar.backgroundColor = [UIColor blueColor];
//設置tabBarItem字體顏色
NSMutableDictionary<NSAttributedStringKey, id> *normalAttributes = [NSMutableDictionary dictionary];
[normalAttributes setValue:[UIColor blueColor] forKey:NSForegroundColorAttributeName];

[self.tabBarItem setTitleTextAttributes:normalAttributes.copy forState:UIControlStateNormal];
[self.tabBarItem setTitleTextAttributes:normalAttributes.copy forState:UIControlStateSelected];

解決方法–重新設置相關屬性

tabBar

UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
//tabBaritem title選中狀態顏色
appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{
    NSForegroundColorAttributeName:[UIColor blueColor],
};
//tabBaritem title未選中狀態顏色
appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{
    NSForegroundColorAttributeName:[UIColor blueColor],
};
//tabBar背景顏色
appearance.backgroundColor = [UIColor blackColor];
self.tabBarItem.scrollEdgeAppearance = appearance;
self.tabBarItem.standardAppearance = appearance;

其中 standardAppearance和scrollEdgeAppearance等的區別

  • standardAppearance — 常規狀態
  • scrollEdgeAppearance — 小屏幕手機橫屏時的狀態
  • scrollEdgeAppearance — 唄scrollview向下拉的狀態

navigationBar

UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
appearance.backgroundColor = [UIColor blackColor];
self.navigationBar.standardAppearance = appearance;
self.navigationBar.scrollEdgeAppearance = appearance;

2、tableview新屬性-sectionHeaderTopPadding

官方支持

/// Determines if the table view allows its cells to become focused.
/// When tableView:canFocusRowAtIndexPath: is implemented, its return value takes precedence over this method.
/// Defaults to a system derived value based on platform and other properties of the table view.
@property (nonatomic, getter=isPrefetchingEnabled) BOOL prefetchingEnabled

iOS 15中tableView會給每一個section的頂部(header以上)再加上一個22像素的高度,形成一個section和section之間的間距

使用

為瞭配合以前的開發習慣,我們隻需要在創建實例的時候進行對間距的設置即可

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

或者全局設置

if (@available(iOS 15.0, *)) {
    [UITableView appearance].sectionHeaderTopPadding = 0;
}

到此這篇關於iOS15適配小結的文章就介紹到這瞭,更多相關iOS15適配內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: