在 iOS 4.2 中未正确绘制子类 UIAlertView

Subclassed UIAlertView not drawn correctly in iOS 4.2(在 iOS 4.2 中未正确绘制子类 UIAlertView)

本文介绍了在 iOS 4.2 中未正确绘制子类 UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 UIAlertView 子类化如下:

I've subclassed an UIAlertView as follow:

@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 经典背景(蓝色圆角矩形).

On the previous versions of iOS (I'm always testing on the simulator) the subclass run fine and when shown it displayed the custom background image drawn correctly and just text upon it, whereas in the 4.2 version it draws the UIAlertView classic background (the blue rounded corners rectangle) on top of my image.

我做错了什么?任何有关 UIAlertView 编程和 UIView 的建议都将不胜感激.

What am I doing wrong? Any suggestion about UIAlertView programming and UIView too will be appreciated.

更新有没有人分享一些 UIAlertView 替换类?

UPDATE Has anyone got some UIAlertView replacement class to share?

推荐答案

我们在 iOS 4.2 中遇到了类似的 UIAlertView 问题;我们正在自定义布局、添加文本框并重新排列按钮.

We had a similar problem with UIAlertView in iOS 4.2; we were customizing the layout, added text boxes and rearranging the buttons.

这不会是一个流行的答案,但由于 UIAlertView 的更改,我们不得不完全放弃使用它来实现此目的.我们一直都知道这是一个脆弱的实现,因为自定义/子类化 UIAlertView 不受官方支持,并且对视图层次结构的内部结构进行了假设,但 4.2 的发布确实让我们开始行动.

This won't be a popular answer, but due to the changes to UIAlertView, we had to abandon using it entirely for this purpose. We always knew it was a fragile implementation since customizing/subclassing UIAlertView isn't officially supported and makes assumptions about the internal structure of the view hierarchy, but the release of 4.2 really kicked us into gear.

最后,我们实现了自己的 UI 元素来替换自定义的 UIAlertView.

In the end, we implemented our own UI element to replace the customized UIAlertView.

这篇关于在 iOS 4.2 中未正确绘制子类 UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 iOS 4.2 中未正确绘制子类 UIAlertView