Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Shadows to MKMapView

I've a MapView, and I want to add it a dropshadow, but the method I tried doesn't work:

- (void)viewDidLoad {
    [super viewDidLoad];

    mapView.layer.shadowColor = [[UIColor blackColor] CGColor]; 
    mapView.layer.shadowOffset = CGSizeMake(10.0f, 10.0f);
    mapView.layer.shadowOpacity = 1.0f;
    mapView.layer.shadowRadius = 10.0f;
}

I get this:

example

Am I doing something wrong?

like image 990
patrick Avatar asked Jan 24 '11 17:01

patrick


2 Answers

Resolved thanks to: http://blog.amarkulo.com/create-rounded-uiviews-with-shadow

using this code:

[[mapView layer] setMasksToBounds:NO];
[[mapView layer] setShadowColor:[UIColor blackColor].CGColor];
[[mapView layer] setShadowOpacity:1.0f];
[[mapView layer] setShadowRadius:6.0f];
[[mapView layer] setShadowOffset:CGSizeMake(0, 3)];
like image 87
patrick Avatar answered Oct 29 '22 17:10

patrick


Another alternative (actually the proposed solution didn't work for me, iOS SDK 4.3) is to enclose the MKMapView in a UIView:

_mapContainer = [[UIView alloc] initWithFrame: CGRectMake (0.0f, 44.0f, 320.0f, container.frame.size.height - 44.0f)];
_mapContainer.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_mapContainer.layer.masksToBounds = NO;
_mapContainer.layer.shadowColor = [UIColor blackColor].CGColor;
_mapContainer.layer.shadowOffset = CGSizeMake (0.0f, 10.0f);
_mapContainer.layer.shadowOpacity = 0.6f;
_mapContainer.layer.shadowRadius = 5.0f;
[container addSubview: _mapContainer];
[_mapContainer release];

_mapView = [[MKMapView alloc] initWithFrame: _mapContainer.bounds];
_mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[_mapContainer addSubview: _mapView];
[_mapView release];

This way you can also animate the frame of the _mapContainer and still keep the shadow in its correct place.

Here you can see actual results here, if you're a registered Apple developer.

like image 29
Lorenzo Bevilacqua Avatar answered Oct 29 '22 18:10

Lorenzo Bevilacqua