I am porting my existing iOS 6 application to the new iOS 7. But whenever a MKMapView instance appears into the screen, my navigation bar loses its tint color.
Steps to reproduce:
Open Xcode;
Create a new Master-Detail Application;
Add the next line as the first one of the AppDelegate.m didFinishLaunchingWithOptions method:
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]
Add the next line to the MasterViewController.m file:
#import <MapKit/MapKit.h>
Finally, add the next lines to the MasterViewController.m cellForRowAtIndexPath method:
MKMapView *view = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
[cell.contentView addSubview:view];
Build and Run
These are the results:

Am I doing something wrong? What can I do to fix this issue?
Should I start using the Google Maps API instead?
I put a MKMapView inside a MyMapViewController and applied auto layout constraints so it took all the available space. Whenever I push that view controller onto a UINavigationController the navigation bar becomes transparent.
My solution was to subclass UINavigationController and apply a fix in the UINavigationControllerDelegate method.
class MainViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationBar.prefersLargeTitles = false
delegate = self
pushMyMapViewController()
}
private func pushMyMapViewController() {
let myMapViewController = MyMapViewController()
pushViewController(myMapViewController, animated: true)
}
}
// MARK: - UINavigationControllerDelegate
extension MainViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if viewController is MyMapViewController {
// Fix the issue with placing a MKMapView underneath the UINavigationBar causing the bar to become transparent.
// This is due to the `scrollEdgeAppearance` change introduced in iOS 15.
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
} else {
navigationBar.scrollEdgeAppearance = nil
}
}
}
You do not necessarily need to subclass UINavigationController. However I found it the best place to set the scrollEdgeAppearance to standardAppearance when the view controller containing the MKMapView is pushed onto the stack and set it back to nil when that view controller is popped off the stack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With