UINavigation color setting, caused problems with the tripping effect

####1. UINavigation 1.1. First we always ignore, not setting, the color of navigation which we always set easily

/ / set a gradient color navigationBar - (void) setNavigationBarBackgroundAlpha: (float) alpha {UIColor * color = [UIColor colorWithWhite:1 alpha:alpha]; UIImage *colorImage = [UIImage imageWithColor:color]; // Attributes added after ios 15, 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]; }Copy the code

1.2 UINavigationBarAppearance

apple developer

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.

Starting with iOS 15, UINavigationBar, UIToolbar, and UITabBar will use scrollEdgeAppearance when your VC associated scroll view is at the appropriate edge. You must customize it using the UIBarAppearance API. UIToolbar and UITabBar have added the scrollEdgeAppearance attribute to iOS 15 for this purpose.

** UITabBar is designed as follows

if #available(iOS 15, *) {
     let appearance = UITabBarAppearance()
     appearance.configureWithOpaqueBackground()
     appearance.backgroundColor = k_FAFAFA
     UITabBar.appearance().standardAppearance = appearance
     UITabBar.appearance().scrollEdgeAppearance = UITabBar.appearance().standardAppearance
}
Copy the code

# # # # 2.1 translucent

3. The background of UINavigationBar, always determined by a combination of barTintColor and backgroundImage, always plays a crucial role. It determines how the system uses bar Color and backgroundImage to set the background effect of the UINavigationBar. 1. Default values (Don’t set3. What is the background behavior of a UINavigationBar always set to YES, as always, if it is not set to either NO or YES, i.e., not actively calling setter methods? As verified by the code, 1. Not actively setting up always is not the same as setting up YES. If you set an opaque backgroundImage, the UINavigationBar background actually looks like an opaque backgroundImage. Printing this value, always becomes NO. This is also the most common case where the ViewController layout actually starts at the lower left corner of the NavigationBar

  1. If a transparent backgroundImage is set, printing always becomes YES

This effect is also commonly seen in ViewController layouts that start at the top left corner of the navigationBar

Problems arisingThe tableView always presents a topping effect as a jump or return from background to foreground, as always changing.

If the backgroundImage is translucent, barTintColor will be used to create an opaque background

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.
Copy the code

2-1. Whether backgroundImage is nil, translucent, or opaque, the system uses barTintColor to set an opaque background. And the alpha value of barTintColor does not work, it is always 1.

The opaque background type is _UIBarBackground, a private class that inherits from UIView and whose background color is set to barTintColor. The UIImageView that holds the backgroundImage is a child view of _UIBarBackground. (No classification and examples here)

2-3. If backgroundImage is translucent, the actual background effect of UINavigationBar is the overlay effect of backgroundImage and barTintColor. If it is opaque, The actual background effect of the UINavigationBar is the background age.

3, actively set to YES 3-1. If the translucent backgroundImage is set, then the actual effect of UINavigationBar background is the backgroundImage effect. If the image is completely transparent, The UINavigationBar background is actually completely transparent.

3-2. If an opaque backgroundImage is set, the system will force the backgroundImage to be translucent. The actual effect of UINavigationBar background is the backgroundImage translucent effect. The system will force change alpha = 0.909804

3-3. If backgroundImage is set, barTintColor will not have any effect.

3-4. If backgroundImage is not set, the system will use barTintColor to set the UINavigationBar background effect, with a little blur effect, no matter what the alpha value of barTintColor is, it will not work. Will be forced to modify alpha=0.85

2.2 translucent value
  1. 3. If actively set, always remains, as always, until again set with a new value.

  2. 3. If NO backgroundImage is always set, the backgroundImage is always determined by YES, or NO. Is always the default YES.