2012-04-06 82 views
14

我用這個好的tutorial來創建一個自定義的UIPopoverBackgroundView類。自定義UIPopoverBackgroundView:沒有投影

它運作良好。唯一的問題是我沒有得到典型的UIPopoverController陰影,我想要它。我試過在我的UIPopoverBackgroundView實例的層上指定它,但沒有成功。我的UIPopoverController實例似乎沒有公共視圖來操作。將它添加到彈出窗口內容也不起作用。

可能很簡單:如何在使用自定義UIPopoverBackgroundView類時添加投影?

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

回答

14

好了,想通了。我需要將投影添加到borderImageView,而不是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

非常感謝你爲這個! – 2012-05-26 17:52:55

+1

只是想我會補充說這對我有效,但我有性能問題。柵格化背景圖層非常有幫助:'backgroundImageView.layer.shouldRasterize = YES;' – Maurizio 2012-07-04 16:26:22

+0

有趣 - 我沒有注意到任何明顯的滯後(在iPad 2上),但很好知道。 – 2012-07-04 16:33:02

19

您不需要添加自己的陰影。基地UIPopoverBackgroundView會爲你做。只要確保在您的layoutSubviews實施中打電話超級。

編輯:我的意見適用於應用定位的iOS 6

+0

+1正確。我驚訝地發現我的自定義彈出窗口在iOS 6上運行後開始放下陰影。 – Mazyod 2012-10-07 13:36:06

+0

感謝您添加更新@ iOS6 - 我在看到您的評論並在'layoutSubviews'上調用super後沒有添加回iOS5下檢查影子。 – 2012-10-08 05:28:32

+0

我在iOS 6和我的UIPopoverBackgroundView不顯示陰影:( – 2012-12-05 08:31:45