iOS15系統(tǒng)需要對導航欄進行適配捻爷,增加兩種appearance,分別是scrollEdgeAppearance
(不滾動時)和standardAppearance
(滾動時)嘿架。iOS15系統(tǒng)之前設置導航欄樣式后滾動和不滾動是沒區(qū)別的松捉,現(xiàn)在要實現(xiàn)滾動和不滾動一樣的樣式必須設置這兩種appearance。
下面在控制器里生成一個appearance并賦值給導航欄:(如果需要設置不同的樣式可以生成兩個appearance對象分別賦值給導航欄)
if (@available(iOS 15.0,*)) {
UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
self.navigationController.navigationBar.standardAppearance = self.navigationController.navigationBar.scrollEdgeAppearance;
}
在需要修改樣式時可以通過導航欄再獲取到這兩個appearance戈次,通過appearance修改樣式轩勘。
注意:
雖然上面生成appearance并且賦值給導航欄時用的是同一個,但是將appearance賦值給導航欄后怯邪,導航欄會復制一份绊寻,所以導航欄的兩種appearance并不是同一個對象。在修改樣式時如需修改兩種狀態(tài)下的導航欄樣式,必須同時獲取到兩種appearance再分別進行修改澄步,只獲取一個修改是不行的1ⅰ!村缸!
下面是在某處修改導航欄樣式:
if (@available(iOS 15.0,*)) {
UINavigationBarAppearance *appearance = self.navigationController.navigationBar.scrollEdgeAppearance;
// 設置為不透明
appearance.backgroundEffect = nil;
appearance.shadowColor = [UIColor clearColor];
appearance.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor]];
UINavigationBarAppearance *appearance2 = self.navigationController.navigationBar.standardAppearance;
// 設置為不透明
appearance2.backgroundEffect = nil;
appearance2.shadowColor = [UIColor clearColor];
appearance2.backgroundImage = [UIImage imageNamed:@"test"];
} else {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"test"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]]; //去掉導航下面的線
}