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

标签 iphone uiview ios4 background uialertview

我对 UIAlertView 进行了子类化,如下所示:

@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 的建议都将受到赞赏。

更新有人有一些 UIAlertView 替换类可以分享吗?

最佳答案

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

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

最终,我们实现了自己的 UI 元素来替换定制的 UIAlertView。

关于iphone - 子类 UIAlertView 在 iOS 4.2 中未正确绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4269461/

相关文章:

iphone - AVAudioPlayer 在模拟器中工作,但不在设备上

ios - ScrollView 理解

iOS,PopOverPresentationController 问题,.xib

ios - 将按钮 (UIView) 绑定(bind)到可在多个文本字段中使用的键盘 (Swift 4.2)

UIScrollView 中的UIView 中的UITableView。滚动 tableview 时不要滚动 scrollview

ios - 将其他 View 移动到隐藏的 View

ios - 处理 applicationDidBecomeActive - "How can a view controller respond to the app becoming Active?"

Objective-C:没有调用 volumeChanged 甚至为 AVSystemController_SystemVolumeDidChangeNotification 添加观察者?

ios - 带每个路线步骤终点坐标的转弯导航

iphone - 使固定大小的圆形按钮作为iPhone SDK中的图像