There is a Swift version of this :).
Simple dim overlay on iOS MKMapView, covering entire map using MKOverlay and MKOverlayView with customizable colour and alpha values.
Initialize and add MKMapDimOverlay
to your MKMapView object.
- (void)addDimOverlay {
MKMapDimOverlay *dimOverlay = [[MKMapDimOverlay alloc] initWithMapView:self.mapView];
[self.mapView addOverlay: dimOverlay];
}
And in map view's mapView: rendererForOverlay:
delegate method, instantiate and return MKMapDimOverlayView
. You can customize overlay color and opacity.
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
if([overlay isMemberOfClass:[MKMapDimOverlay class]]) {
MKMapDimOverlayView *dimOverlayView = [[MKMapDimOverlayView alloc] initWithOverlay:overlay];
dimOverlayView.overlayAlpha = 0.3;
dimOverlayView.overlayColor = [UIColor magentaColor];
return dimOverlayView;
}
return nil;
}
CocoaPods is the recommended way to add MKMapDimOverlay to your project.
- Add a pod entry for MKMapDimOverlay to your Podfile
pod 'MKMapDimOverlay'
- Install the pod(s) by running
pod install
. - Include MKMapDimOverlay wherever you need it with
#import "MKMapDimOverlay.h"
and#import "MKMapDimOverlayView.h"
.
Alternatively you can directly add the MKMapDimOverlay.h
, MKMapDimOverlay.m
, MKMapDimOverlayView.h
and MKMapDimOverlayView.m
source files to your project.
- Download the latest code version or add the repository as a git submodule to your git-tracked project.
- Open your project in Xcode, then drag and drop
MKMapDimOverlay.h
,MKMapDimOverlay.m
,MKMapDimOverlayView.h
andMKMapDimOverlayView.m
onto your project (use the "Product Navigator view"). Make sure to select Copy items when asked if you extracted the code archive outside of your project. - Include MKMapDimOverlay wherever you need it with
#import "MKMapDimOverlay.h"
and#import "MKMapDimOverlayView.h"
.
You can also add MKMapDimOverlay as a static library to your project or workspace.
- Download the latest code version or add the repository as a git submodule to your git-tracked project.
- Open your project in Xcode, then drag and drop
MKMapDimOverlay.xcodeproj
onto your project or workspace (use the "Product Navigator view"). - Select your target and go to the Build phases tab. In the Link Binary With Libraries section select the add button. On the sheet find and add
libMKMapDimOverlay.a
. You might also need to addMKMapDimOverlay
to the Target Dependencies list. - Include MKMapDimOverlay wherever you need it with
#import "MKMapDimOverlay.h"
and#import "MKMapDimOverlayView.h"
.
This code is distributed under the terms and conditions of the MIT license.