UINavigation 的顏色設置,引起的吸頂效果的問題
1、UINavigation 的顏色設置
1.1、首先我們忽略translucent 屬性,不要設置它,
我們設置navigation的顏色會很容易
//設置一個漸變顏色的navigationBar
- (void)setNavigationBarBackgroundAlpha:(float)alpha{
UIColor *color = [UIColor colorWithWhite:1 alpha:alpha];
UIImage *colorImage = [UIImage imageWithColor:color];
//ios 15以后新增的屬性,以后設置navigationBar的顏色和字體都 要使用它了
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
appearance.backgroundColor = color;//[UIColor colorWithHexString:@"#ad0225" alpha:alpha];
appearance.shadowColor = color;//[UIColor colorWithHexString:@"#ad0225" alpha:alpha];
[appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithWhite:0 alpha:alpha]}];
if(alpha == 0){
self.navigationController.navigationBar.standardAppearance = nil;
self.navigationController.navigationBar.scrollEdgeAppearance = nil;
}else{
self.navigationController.navigationBar.standardAppearance = appearance;
self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
}
}
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithWhite:0 alpha:alpha]}];
[self.navigationController.navigationBar setShadowImage:colorImage];
[self.navigationController.navigationBar setBackgroundImage:colorImage forBarMetrics:UIBarMetricsDefault];
// [self.navigationController.navigationBar setTintColor:color];
}
1.2 UINavigationBarAppearance
As of iOS 15, UINavigationBar, UIToolbar, and UITabBar will use their scrollEdgeAppearance when your view controller's associated scroll view is at the appropriate edge (or always if you don't have a UIScrollView in your hierarchy, more on that below)
--
You must adopt the UIBarAppearance APIs (available since iOS 13, specializations for each bar type) to customize this behavior. UIToolbar and UITabBar add scrollEdgeAppearance properties for this purpose in iOS 15.
從 iOS 15 開始,UINavigationBar、UIToolbar 和 UITabBar 將在你的VC關聯滾動視圖位于適當的邊緣時使用 scrollEdgeAppearance
您必須使用 UIBarAppearance API 來自定義。UIToolbar 和 UITabBar 為此在 iOS 15 中添加了 scrollEdgeAppearance 屬性。
** UITabBar 設計如下
if #available(iOS 15, *) {
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = k_FAFAFA
UITabBar.appearance().standardAppearance = appearance
UITabBar.appearance().scrollEdgeAppearance = UITabBar.appearance().standardAppearance
}
2.1 translucent
UINavigationBar的背景由translucent、barTintColor、backgroundImage三者共同決定,而translucent在中間起非常關鍵的作用,它決定了系統如何使用barTintColor和backgroundImage去設置UINavigationBar的背景效果。
1、默認值(不設置)
translucent系統默認值為YES,如果既不設置為NO,也不設置為YES,即不主動調用setter方法,UINavigationBar的背景行為特性又會怎樣呢?通過代碼驗證,結論是:
1、不主動設置translucent和主動把translucent設置為YES是不一樣的,
-
如果設置了不透明的backgroundImage,UINavigationBar背景實際效果就是不透明的backgroundImage。此時打印translucent的值,會發現變成了NO。 這種情況也是我們最常見的情況,此時ViewController的布局其實從navigationbar 的 左下角下開始
16398142837132.jpg 如果設置了透明的backgroundImage,打印translucent的值,會發現又變成了YES
這種效果我們也經常見 這種ViewController的布局是從navigationBar 的左上角開始布局的
[圖片上傳失敗...(image-13c96a-1639817019029)]
引起的問題
tableView 吸頂效果的展示會隨著translucent 值的變化在跳轉頁面或者從后臺返回前臺的時候,
2、 主動設置成NO
蘋果官方解析 如果設置為NO,backgroundImage若是半透明的圖,則會使用barTintColor為backgroundImage創建一個不透明的背景
If you send setTranslucent:NO to a bar with a translucent custom background image
it will provide an opaque background for the image using the bar's barTintColor if defined,
or black for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
2-1. 無論backgroundImage是nil、半透明的圖,還是不透明的圖,系統都會使用barTintColor來設置一個不透明的背景。且barTintColor的alpha值不起作用,一直為1。
2-2. 不透明的背景類型是_UIBarBackground,一個私有類,繼承自UIView,它的背景色被設置成了barTintColor。承載backgroundImage的UIImageView是_UIBarBackground的子視圖。(這里暫且不分類和實例)
2-3. 如果backgroundImage是半透明,則UINavigationBar的實際背景效果就是backgroundImage和barTintColor疊加的效果,如果是不透明,則UINavigationBar的實際背景效果就是backgroundImage。
3、主動設置成YES
3-1. 如果設置了半透明的backgroundImage,那么UINavigationBar背景實際效果就是backgroundImage的效果,如果是完全透明的圖片,那UINavigationBar背景實際效果就是完全透明。
3-2. 如果設置了不透明的backgroundImage,那系統會強制將backgroundImage改為半透明,UINavigationBar背景實際效果就是backgroundImage半透明的效果;系統會強制修改的alpha = 0.909804
3-3.如果設置了backgroundImage,那barTintColor將不起任何效果。
3-4. 如果沒有設置backgroundImage,那系統會使用barTintColor來設置UINavigationBar背景效果,而且帶有一點模糊效果,無論barTintColor的alpha值是多少,都不起作用,會被強制修 alpha=0.85
2.2 translucent 值
如果主動設置translucent,那translucent就會一直是設置的那個值,除非再次主動設置新值。
如果不主動設置translucent,那translucent的值由backgroundImage決定,backgroundImage透明則為YES,backgroundImage不透明則為NO,如果沒有backgroundImage,則一直是默認的YES。