簡體   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