Search code examples
ios6

wierd shadow under UIView


I have this simple code:

UIView* shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, 5)];
shadowView.backgroundColor = [UIColor whiteColor];
shadowView.layer.masksToBounds = NO;
shadowView.layer.shadowOffset = CGSizeMake(0, 0);
shadowView.layer.shadowColor = [[UIColor blackColor] CGColor];
[shadowView.layer setShouldRasterize:YES];
shadowView.layer.shadowOpacity = 1;
[self.view addSubview:shadowView];

This creates a white view with shadow on top and bottom. Both the shadows are oriented nicely, with a gradient from top to bottom.

If I make this change:

shadowView.layer.shadowOffset = CGSizeMake(0, 5);

then the shadow appears only on the bottom (as I wanted) but the gradient is lost somehow. I think the top overlaps the bottom gradient.

How do I make it cast a shadow only on the bottom? (this is under IOS6).

Basically I want to create this image:

enter image description here


Solution

  • Apparently this is just the way shadows work on ios6. The problem was that I was setting a an offset (0, 5) where the 5 was simply larger than the line. After playing with the values for a while, I realized you can cast a shadow downwards, as long as you don't try to get a shadow larger than the image casting the shadow.