繁体   English   中英

如何在iPhone中为UIAlert视图设置背景图像?

[英]How to set the Background image for UIAlert view in iPhone?

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

这是我的示例代码,

   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

看到我的屏幕截图:

图片

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

您必须使用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 ([subview isMemberOfClass:[UIImageView class]])
    {
        [subview removeFromSuperview];
    }
}

您是否尝试过:

_backgroundImageView.frame = notesAlert.frame;

您可以在UIAlertView的委托方法alertviewwillappear出现在uiimageview框架中设置框架。 或者,您可以使用UIPopoverControllerUIPopoverController添加bgimage。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM