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 vi开发者_JAVA百科ewDidLoad];
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:
Am I doing something wrong?
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)];
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.
精彩评论