2012-04-06 18 views
14

He usado esta buena tutorial para crear una clase UIPopoverBackgroundView personalizada.UIPopoverBackgroundView personalizado: sin sombra paralela

Funciona bien. El único problema es que no obtengo la sombra paralela típica de UIPopoverController y la quiero. Intenté especificarlo en la capa de mi instancia de UIPopoverBackgroundView sin éxito. Mi instancia de UIPopoverController no parece tener una vista pública para manipular. Agregarlo al contenido de popover tampoco funciona.

Probablemente muy simple: ¿cómo agrego una sombra paralela cuando uso una clase UIPopoverBackgroundView personalizada?

// UIPopoverBackgroundView.m

-(id)initWithFrame:(CGRect)frame{ 
    if (self = [super initWithFrame:frame]) { 
     _borderImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"bg-popover.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(CAP_INSET,CAP_INSET,CAP_INSET,CAP_INSET)]]; 

     _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-popover-arrow.png"]]; 

     [self addSubview:_borderImageView]; 
     [self addSubview:_arrowView]; 

     self.layer.shadowOffset = CGSizeMake(50, 50); 
     self.layer.shadowColor = [[UIColor blackColor] CGColor]; 
    } 

    return self; 
} 

Respuesta

14

Ok, lo descubrió. Necesitaba agregar la sombra paralela al borderImageView, no la vista de la instancia de popover.

- (id)initWithFrame:(CGRect)frame 
{ 
    if (self = [super initWithFrame:frame]) { 
     _borderImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"bg-popover.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(CAP_INSET,CAP_INSET,CAP_INSET,CAP_INSET)]]; 

     _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-popover-arrow.png"]]; 

     [self addSubview:_borderImageView]; 
     [self addSubview:_arrowView]; 

     _borderImageView.layer.cornerRadius = 5.0f; 
     _borderImageView.layer.masksToBounds = NO; 
     _borderImageView.layer.borderWidth = 1.0f; 
     _borderImageView.layer.borderColor = [UIColor blackColor].CGColor; 

     _borderImageView.layer.shadowColor = [UIColor blackColor].CGColor; 
     _borderImageView.layer.shadowOpacity = 0.8; 
     _borderImageView.layer.shadowRadius = 50; 
     _borderImageView.layer.shadowOffset = CGSizeMake(-10.0f, 10.0f); 
    } 

    return self; 
} 
+0

muchas gracias por esto! –

+1

Solo pensé que agregaría que esto funcionó para mí, pero tuve problemas de rendimiento. Rasterizar la capa de fondo ayudó inmensamente: 'backgroundImageView.layer.shouldRasterize = YES;' – Maurizio

+0

Interesante - No noté ningún retraso significativo (en iPad 2) pero es bueno saberlo. –

19

No necesita agregar sus propias sombras. La base UIPopoverBackgroundView lo hará por usted. Solo asegúrese de llamar a super en su implementación layoutSubviews.

EDIT: Mi comentario se aplica a las aplicaciones de iOS 6. dirigidos

+0

+1 cierto. Vine aquí después de sorprenderme de que mi popover personalizado comenzara a dejar sombras después de ejecutarlo en iOS 6 – Mazyod

+0

Gracias por agregar la actualización @ iOS6 - Había vuelto a verificar en iOS5 después de ver tu comentario y llamar super en 'layoutSubviews' no se agregó la sombra. –

+0

Estoy en iOS 6 y mi UIPopoverBackgroundView no muestra la sombra :( –

Cuestiones relacionadas