2010-11-24 97 views
2

正確繪製我子類的UIAlertView中如下:子類UIAlertView中沒有的iOS 4.2

@interface NarrationAlertView : UIAlertView { 
    UIImage * backgroundImage; //The image I want as custom background 
    UILabel * textualNarrationView; //The test I wanna be displayed on the view   
} 

並實現了這種方式:

- (id)initNarrationViewWithImage:(UIImage *)image{ 

    if (self = [super init]){ 
     UILabel * alertTextLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     self.textualNarrationView = alertTextLabel; 
     [alertTextLabel release]; 
     [self addSubview:alertTextLabel]; 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 
    /* Here I draw the image as background */ 
    CGSize imageSize = self.backgroundImage.size; 
    [self.backgroundImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)]; 
} 

- (void)layoutSubviews { 
    /* To fit the text */ 
    [textualNarrationView sizeToFit]; 

    CGRect textRect = textualNarrationView.frame; 
    textRect.origin.x = (CGRectGetWidth(self.bounds) - CGRectGetWidth(textRect))/2; 
    textRect.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(textRect))/2; 
    textRect.origin.y -= 70; 
    textualNarrationView.frame = textRect; 
} 

- (void)show{ 
    /* Showing the view */ 
    [super show]; 
    CGSize imageSize = self.backgroundImage.size; 
    self.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height); 
} 

在iOS上的以前的版本(我總是在模擬器上測試)子類運行良好,並在顯示時顯示正確繪製的自定義背景圖像,並且就是其上的文本,而在4.2版本中,它繪製了UIAlertView經典背景(藍色圓角矩形)頂部我的形象。

我在做什麼錯?不勝感激任何有關UIAlertView編程和UIView的建議。

UPDATE是否有人有一些UIAlertView替換類共享?

回答

5

我們在iOS 4.2中遇到了與UIAlertView類似的問題;我們正在自定義佈局,添加文本框和重新排列按鈕。

這不會是一個受歡迎的答案,但由於UIAlertView的更改,我們不得不放棄將其完全用於此目的。我們總是知道這是一個脆弱的實現,因爲UIAlertView的自定義/子類化並沒有得到官方支持,並且對視圖層次結構的內部結構進行了假設,但是4.2的發佈真的使我們陷入了困境。

最後,我們實現了我們自己的UI元素來替換自定義的UIAlertView。

+0

我擔心的是,你怎麼樣完成的彈出效果與背景陰影視景? – rano 2010-11-24 20:01:31

+0

實際上,我們對UI採取了不同的方式,過去的彈出窗口現在從屏幕頂部向下滑動/向上滑動。我確實研究了複製彈出效果,並發現這個有用的鏈接:http://delackner.com/blog/2009/12/mimicking-uialertviews-animated-transition/ – 2010-11-24 20:04:30

3

在iOS 4.2之前UIAlertView的標準深藍色圓角矩形繪製在drawRect中。通過繼承UIAlertView並在不調用super的情況下實現drawRect,可以刪除圓角矩形。然而在4.2中,圓角矩形是一個UIImageView子視圖。 快速,輕鬆(不最好的)解決方案:如果你不加入任何的UIImageView實例您UIAlertView中的子類,你可以簡單地刪除通過觀察子視圖添加加載默認的UIImageView:

- (void)didAddSubview:(UIView *)subview { 
    if ([subview isMemberOfClass:[UIImageView class]]) { 
    [subview removeFromSuperview]; 
    } 
}