2010-11-24 93 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]; 
    } 
}