title: Setting the Navigation Bar in iOS 15
tags:
- Technology
- iOS
date: 2021-09-03 10:38#
Setting the Navigation Bar in iOS 15#
Background#
When running a project on iOS 15 using Xcode 13.0, the navigation bar appears black. However, there are no issues when running on lower versions of Xcode.
Modification#
The method of setting the navigation bar needs to be modified, referring to barTintColor not working in iOS 15.
The original code for setting the navigation bar remains unchanged. Instead, new properties of the UINavigationBarAppearance instance object need to be set and assigned to the global navigationBar or the navigationBar property of individual pages, depending on whether the project's settings are for a global NavigationBar or for individual page settings (refer to iOS StatusBar Settings).
The code is as follows:
- (void)updateNavigationBarColor:(UIColor *)color {
UINavigationBar *bar = self.navigationController.navigationBar;
if (@available(iOS 13.0, *)) {
UINavigationBarAppearance *barAppearance = [UINavigationBarAppearance new];
barAppearance.backgroundColor = color; // Set background color
barAppearance.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:17]}; // Set navigation bar font color and size
barAppearance.shadowColor = [UIColor clearColor]; // Hide the bottom separator line of the navigation bar
bar.scrollEdgeAppearance = bar.standardAppearance = barAppearance;
[bar setShadowImage:[UIImage new]];
} else {
// Fallback on earlier versions
}
[bar setBackgroundImage:[UIImage wps_createImageWithColor:color] forBarMetrics:UIBarMetricsDefault];
}