2012-04-06 13 views
14

Ho utilizzato questo buono tutorial per creare una classe UIPopoverBackgroundView personalizzata.UIPopoverBackgroundView personalizzato: nessuna ombra esterna

Funziona bene. L'unico problema è che non ottengo il tipico drop shadow di UIPopoverController e lo voglio. Ho provato a specificarlo sul livello della mia istanza UIPopoverBackgroundView senza successo. La mia istanza di UIPopoverController non sembra avere una vista pubblica da manipolare. Anche aggiungerlo al contenuto del popover non funziona.

Probabilmente davvero semplice: come si aggiunge un'ombra esterna quando si utilizza una classe UIPopoverBackgroundView personalizzata?

// 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; 
} 

risposta

14

Ok, capito. Dovevo aggiungere l'ombra esterna allo borderImageView, non alla vista dell'istanza di 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

Grazie tanto per questo! –

+1

Ho solo pensato di aggiungere che questo ha funzionato per me, ma ho avuto problemi di prestazioni. Rasterizzare il livello di sfondo ha aiutato immensamente: 'backgroundImageView.layer.shouldRasterize = YES;' – Maurizio

+0

Interessante - Non ho notato alcun ritardo significativo (su iPad 2) ma buono a sapersi. –

19

Non è necessario aggiungere le proprie ombre. La base UIPopoverBackgroundView lo farà per te. Assicurati di chiamare super nell'implementazione layoutSubviews.

EDIT: Il mio commento si applica alle applicazioni rivolte iOS 6.

+0

+1 vero. Sono venuto qui dopo essere stato sorpreso dal fatto che il mio popover personalizzato ha iniziato a rilasciare ombre dopo averlo eseguito su iOS 6 – Mazyod

+0

Grazie per aver aggiunto l'aggiornamento @ iOS6 - Sono tornato a controllare su iOS5 dopo aver visto il tuo commento e la chiamata super su 'layoutSubviews' non ha aggiunto l'ombra. –

+0

Sono in iOS 6 e il mio UIPopoverBackgroundView non mostra l'ombra :( –

Problemi correlati