在iOS 13 UINavigationBar
新增了scrollEdgeAppearance
屬性,但在iOS 14及更早的版本中此屬性只應用在大標題導航欄上。在iOS 15中此屬性適用于所有導航欄。
對于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
類型,有下面幾個屬性:
backgroundEffect:
基于
backgroundColor
或backgroundImage
的磨砂效果
backgroundColor:
背景色,在
backgroundImage
之下
backgroundImage:
背景圖片
backgroundImageContentMode:
渲染
backgroundImage
時使用的內容模式。 默認為UIViewContentModeScaleToFill
。
shadowColor:
陰影顏色(底部分割線),當
shadowImage
為nil
時,直接使用此顏色為陰影色。如果此屬性為nil
或clearColor
(需要顯式設置),則不顯示陰影。如果
shadowImage
包含 template 圖像,則使用該圖像作為陰影并使用此屬性中的值對其進行著色。如果此屬性為nil
或clearColor
(需要顯式設置),則不顯示陰影。
但是,如果shadowImage
不包含 template 圖像,則此屬性無效。
shadowImage:
陰影圖片。
template圖像:[img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
示例代碼:
- 不透明導純色航欄:
//navigation標題文字顏色
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];
- 透明導航欄:
因為(更新Xcode13.4.1后和之前似乎不一樣)scrollEdgeAppearance = nil
,當前如果有ScrollView
,當ScrollView
向上滾動時scrollEdgeAppearance
會默認使用standardAppearance
。因此backgroundEffect
和shadowColor
也要顯式設置為nil
,防止backgroundEffect
、shadowColor
出現變成有顏色的。
//navigation標題文字顏色
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];
- 透明導航欄時動態修改顏色:
-(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
時設置 backgroundColor = .clear
沒有過渡動畫,可以改變顏色透明度alpha
來實現。比如可以寫成backgroundColor = UIColor(white: 1, alpha: 0)