
在iOS 13 UINavigationBar新增了scrollEdgeAppearance屬性,但在iOS 14及更早的版本中此屬性只應(yīng)用在大標(biāo)題導(dǎo)航欄上。在iOS 15中此屬性適用于所有導(dǎo)航欄。
對(duì)于scrollEdgeAppearance屬性的說明:
When a navigation controller contains a navigation bar and a scroll view, part of the scroll view’s content appears underneath the navigation bar. If the edge of the scrolled content reaches that bar, UIKit applies the appearance settings in this property.
If the value of this property isnil, UIKit uses the settings found in the standard Appearance property, modified to use a transparent background. If no navigation controller manages your navigation bar, UIKit ignores this property and uses the standard appearance of the navigation bar.
scrollEdgeAppearance是UINavigationBarAppearance類型,有下面幾個(gè)屬性:
backgroundEffect:
基于
backgroundColor或backgroundImage的磨砂效果
backgroundColor:
背景色,在
backgroundImage之下
backgroundImage:
背景圖片
backgroundImageContentMode:
渲染
backgroundImage時(shí)使用的內(nèi)容模式。 默認(rèn)為UIViewContentModeScaleToFill。
shadowColor:
陰影顏色(底部分割線),當(dāng)
shadowImage為nil時(shí),直接使用此顏色為陰影色。如果此屬性為nil或clearColor(需要顯式設(shè)置),則不顯示陰影。如果
shadowImage包含 template 圖像,則使用該圖像作為陰影并使用此屬性中的值對(duì)其進(jìn)行著色。如果此屬性為nil或clearColor(需要顯式設(shè)置),則不顯示陰影。
但是,如果shadowImage不包含 template 圖像,則此屬性無效。
shadowImage:
陰影圖片。
template圖像:[img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
示例代碼:
- 不透明導(dǎo)純色航欄:
//navigation標(biāo)題文字顏色
NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName : [UIFont systemFontOfSize:18 weight:UIFontWeightMedium]};
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
barApp.backgroundColor = [UIColor whiteColor];
barApp.shadowColor = [UIColor whiteColor];
barApp.titleTextAttributes = dic;
self.navigationController.navigationBar.scrollEdgeAppearance = barApp;
self.navigationController.navigationBar.standardAppearance = barApp;
}else{
//背景色
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = dic;
[self.navigationBar setShadowImage:[UIImage new]];
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
}
//不透明
self.navigationController.navigationBar.translucent = NO;
//navigation控件顏色
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
- 透明導(dǎo)航欄:
因?yàn)?code>scrollEdgeAppearance = nil,當(dāng)前如果有(更新Xcode13.4.1后和之前似乎不一樣)ScrollView,當(dāng)ScrollView向上滾動(dòng)時(shí)scrollEdgeAppearance會(huì)默認(rèn)使用standardAppearance。因此backgroundEffect和shadowColor也要顯式設(shè)置為nil,防止backgroundEffect、shadowColor出現(xiàn)變成有顏色的。
//navigation標(biāo)題文字顏色
NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont systemFontOfSize:18]};
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
barApp.backgroundColor = [UIColor clearColor];
barApp.titleTextAttributes = dic;
barApp.backgroundEffect = nil;
barApp.shadowColor = nil;
self.navigationController.navigationBar.scrollEdgeAppearance = barApp;
self.navigationController.navigationBar.standardAppearance = barApp;
}else{
self.navigationController.navigationBar.titleTextAttributes = dic;
[self.navigationBar setShadowImage:[UIImage new]];
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
}
//透明
self.navigationController.navigationBar.translucent = YES;
//navigation控件顏色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
- 透明導(dǎo)航欄時(shí)動(dòng)態(tài)修改顏色:
-(Void)setNavigationBarBackgroundColor:(UIColor *)color {
if (@available(iOS 15.0, *)) {
self.navigationController.navigationBar.standardAppearance.backgroundColor = color
self.navigationController.navigationBar.scrollEdgeAppearance.backgroundColor = color
}else{
self.navigationController.navigationBar.barTintColor = color
}
}
2024年04月12日
push 時(shí)設(shè)置 backgroundColor = .clear沒有過渡動(dòng)畫,可以改變顏色透明度alpha來實(shí)現(xiàn)。比如可以寫成backgroundColor = UIColor(white: 1, alpha: 0)