ios 导航栏新特性

有些时间没有写ios了 , 在ios13以后导航栏有了新的特性; 可以设置不同的状态了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
Fallback Behavior:
1) Appearance objects are used in whole – that is, all values will be sourced entirely from an
instance of UINavigationBarAppearance defined by one of these named properties (standardAppearance,
compactAppearance, scrollEdgeAppearance, compactScrollEdgeAppearance) on either UINavigationBar
(self) or UINavigationItem (self.topItem).
2) The navigation bar will always attempt to use the most relevant appearance instances first,
before falling back to less relevant ones. The fallback logic is:
AtScrollEdge: self.topItem.scrollEdgeAppearance => self.scrollEdgeAppearance
=> self.topItem.standardAppearance => self.standardAppearance
AtCompactScrollEdge: self.topItem.compactScrollEdgeAppearance
=> self.compactScrollEdgeAppearance => self.topItem.scrollEdgeAppearance
=> self.scrollEdgeAppearance => self.topItem.compactAppearance
=> self.compactAppearance => self.topItem.standardAppearance => self.standardAppearance
CompactSize: self.topItem.compactAppearance => self.compactAppearance
=> self.topItem.standardAppearance => self.standardAppearance
NormalSize: self.topItem.standardAppearance => self.standardAppearance
*/

/// Describes the appearance attributes for the navigation bar to use when it is displayed with its standard height.
@property (nonatomic, readwrite, copy) UINavigationBarAppearance *standardAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(13.0), tvos(13.0));
/// Describes the appearance attributes for the navigation bar to use when it is displayed with its compact height. If not set, the standardAppearance will be used instead.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *compactAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(13.0));
/// Describes the appearance attributes for the navigation bar to use when an associated UIScrollView has reached the edge abutting the bar (the top edge for the navigation bar). If not set, a modified standardAppearance will be used instead.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *scrollEdgeAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(13.0));
/// Describes the appearance attributes for the navigation bar to use when it is displayed with its compact heights, and an associated UIScrollView has reached the edge abutting the bar. If not set, first the scrollEdgeAppearance will be tried, and if that is nil then compactAppearance followed by a modified standardAppearance.
@property(nonatomic,readwrite, copy, nullable) UINavigationBarAppearance *compactScrollEdgeAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(15.0));

翻译过来大概会有4个状态: 对应4中情况

  1. standardAppearance : 标准高度需要展示的外观
  2. compactAppearance : 紧凑高度需要展示的外观
  3. scrollEdgeAppearance : 描述当关联的UIScrollView到达与导航栏相邻的边缘(导航栏的顶部边缘)时要使用的导航栏的外观属性。
  4. 描述当导航栏以紧凑的高度显示,并且关联的UIScrollView已到达与导航栏相邻的边缘时,要使用的导航栏的外观属性。如果未设置,将首先尝试scrollEdgeAppearance,如果为零,则compactAppearance后面跟着修改后的standardAppearance

简单说, 就是需要管理者4中状态了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 创建一个UINavigationBarAppearance实例
UINavigationBarAppearance *navBarAppearance = [[UINavigationBarAppearance alloc] init];

// 设置导航栏的背景颜色
navBarAppearance.backgroundColor = RGB16Color(APP_TIN_COLOR);

// 定义标题文本属性
NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
[navBarAppearance setTitleTextAttributes:titleTextAttributes];

// 应用样式到导航栏
UINavigationController *navigationController = self;
[navigationController.navigationBar setStandardAppearance:navBarAppearance];
[navigationController.navigationBar setScrollEdgeAppearance:navBarAppearance];


这个解决了老项目运行的时候 , 顶部导航栏 默认是白色的 , 滑动之后显示为你需要的颜色 …

解决 tabbar 背景透明的问题

1
2
3
4
5
6
if (@available(iOS 13.0,*)){
UITabBarAppearance *tabearance = [[UITabBarAppearance alloc] init];
tabearance.backgroundColor = UIColor.whiteColor;

[navigationController.tabBarItem setStandardAppearance:tabearance];
}

特别注意: setStandardAppearance方法是一样的名称, 但是传参是不一样的对象