Pregunta

En una de mis vistas, estoy agregando sombra a una vista.La cosa es que la sombra muestra espacios blancos en los bordes izquierdo y derecho.Quiero eliminar estos espacios.

Aquí está mi código:

UIView *myView = [[ISTView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 35)];
myView.backgroundColor = [UIColor greyColor];
[myView.layer setShadowOffset:CGSizeMake(0.0, 5.0)];
[myView.layer setShadowOpacity:0.8];
[myView.layer setShadowRadius:2.0];
[myView.layer setShadowColor:[UIColor blackColor].CGColor];
[self.view addSubview:myView];
[myView release];

Aquí está la O / P de mi vista:

ingrese la descripción de la imagen aquí

¿Fue útil?

Solución

If you want homogenous shadow without side effects you can draw it in graphics editor, save to png and place UIImageView with stretchable image on your view. And don't forget to set clipToBounds to NO.

UIView *myView = [[ISTView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 35)];
myView.backgroundColor = [UIColor greyColor];
myView.clipToBounds = NO;

UIImageView *shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 35, 320, 10)];
shadowView.image = [UIImage imageWithName:@"my-shadow.png"];
[myView addSubview:shadowView];
[shadowView release];

[self.view addSubview:myView];
[myView release];

It would be cheaper for system to draw cached existing image above view hierarcy than calculate layer's shadow.

Otros consejos

Use shadowPath to make the shadow larger then the view

view.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, view.frame.size.width+30, view.frame.size.height)].CGPath;

One solution I could think of and is working also is to adjust the view's frame by 2 pixels in X position and width:

UIView *myView = [[ISTView alloc] initWithFrame:CGRectMake(-2.0, 0.0, 324.0, 35)];

But this is not a cleaner approach of doing this. If anyone has better solution, please guide.

Try this, remove the code: [myView.layer setShadowRadius:2.0];

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top