【问题标题】:How to set the Background image for UIAlert view in iPhone?如何在 iPhone 中为 UIAlert 视图设置背景图像?
【发布时间】:2011-06-25 14:52:30
【问题描述】:

我想将自定义图像设置为警报视图的背景。如果我将图像视图帧大小设置为静态,则它可以正确显示。但是我已经通过消息内容是动态显示的,所以有时内容很小,有时它会在警报视图中显示大量数据。那么我如何设置取决于警报视图框架的图像视图框架大小尺寸。

这是我的示例代码,

   UIAlertView *notesAlert = [[UIAlertView alloc] initWithTitle:@"" message:notes delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];

   UIImage *alertBoxImage = [UIImage imageNamed:@"AlertViewBG.png"];

   UIImageView *_backgroundImageView = [[UIImageView alloc] initWithImage:alertBoxImage];

   _backgroundImageView.frame = CGRectMake(0, 0, 282, 130);

   _backgroundImageView.contentMode = UIViewContentModeScaleToFill;

   [notesAlert addSubview:_backgroundImageView];

   [notesAlert sendSubviewToBack:_backgroundImageView]; 

   NSLog(@"The Alert view frame height %f and width %f", notesAlert.frame.size.height, notesAlert.frame.size.width); 

     // height 0.000000 and width 0.000000

   [notesAlert show]; 

   NSLog(@"After showing the Alert view frame height %f and width %f", notesAlert.frame.size.height, notesAlert.frame.size.width); 

    // height 173.800003 and width 312.399994

看我的屏幕截图:

那么如何设置取决于警报视图框架的图像框架大小? 请帮帮我。 谢谢。

【问题讨论】:

    标签: iphone uialertview


    【解决方案1】:

    您可以在其委托方法alertviewwillappear 中将 UIAlertView 的框架设置为 uiimageview 框架。 或者您可以使用 UIPopoverController 并为此添加 bgimage。

    【讨论】:

    • UIPopOverController 据说专门用于 iPad 设备。尝试在其他设备上创建一个会导致异常。还有其他选择吗?
    【解决方案2】:

    您必须使用 UIAlertView 的子类,其中覆盖以下方法...

    - (void)drawRect:(CGRect)rect { UIImage *alertBoxImage = [UIImage imageNamed:@"AlertViewBG.png"]; CGSize imageSize = alertBoxImage.size; [self.imgBackground drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)]; }

    隐藏 UIAlertView 的默认背景图像,如屏幕截图所示

    - (void)didAddSubview:(UIView *)subview { if ([子视图 isMemberOfClass:[UIImageView 类]]) { [子视图 removeFromSuperview]; } }

    【讨论】:

      【解决方案3】:

      【讨论】:

        【解决方案4】:

        你试过了吗:

        _backgroundImageView.frame = notesAlert.frame;
        

        【讨论】:

        • 我试过那个,但没有运气。请参阅我更新的问题。谢谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-11
        • 2010-11-25
        • 2013-10-28
        相关资源
        最近更新 更多