1

I'm having an issue when I attempt to add a drop shadow to an MKMapView's layer, in order for the shadow to be visible I have to set the view's clipsToBounds property to false. However, doing so causes the map tiles to draw outside the view's boundaries, overlapping the shadow and cutting of parts of my view. The result looks something like this:

A screenshot of the bug taken in the iPhone 5.1 Simulator

I'm wondering if there's a way to draw a drop shadow without disabling bounds clipping or otherwise get the drop shadow to appear without this ugly visual bug. My code for setting the drop shadow looks like this:

self.mapView.layer.borderWidth = 5.0;
self.mapView.layer.borderColor = [[UIColor whiteColor] CGColor];
self.mapView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
self.mapView.layer.shadowColor = [[UIColor blackColor] CGColor];
self.mapView.layer.shadowRadius = 5.0;
self.mapView.layer.shadowOpacity = 0.2;
self.mapView.clipsToBounds = NO;
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
rpowell
  • 1,464
  • 2
  • 16
  • 29

2 Answers2

4

Since the layer is owned by the MKMapView, it’s generally not a great idea to be touching it yourself. (This is the kind of thing that’s likely to break in weird ways in later OS versions, and behave in unpredictable ways (it’d be interesting to see if that even works at all with the new iOS 6 3D maps). With layer-backed views on OS X, you’re not supposed to touch the layer directly at all (unless it’s layer-hosting view, but that’s a different discussion))

To get a shadow underneath, just make your own new CALayer with a shadow positioned underneath the map. Alternatively nest the MKMapView as a subview of your own UIView, and add the shadow to your view (that has no need for clipping) instead.

DouglasHeriot
  • 1,674
  • 2
  • 13
  • 19
  • Works perfectly, although at the moment I have all this code in my view controller's `viewDidLoad` function, is there a better place to put it? – rpowell Jul 12 '12 at 11:08
  • `viewDidLoad` is probably not too bad. If it’s getting ridiculous, you just might consider subclassing things or adding more controllers. It really depends on other things. – DouglasHeriot Jul 12 '12 at 12:38
2

You have to create two views, one for the shadow and an other for the rounded corners.

More info here : UIView Round Corners with Shadow

Community
  • 1
  • 1
iSofTom
  • 1,718
  • 11
  • 15