2010-02-24 37 views

回答

42

你可以不喜欢它:

UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)]; 

    NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"smile.png"]]; 
    UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path]; 
    [imageView setImage:bkgImg]; 

    [successAlert addSubview:imageView]; 

    [successAlert show]; 

这将在您的alertview的右角添加一个图像,您可以更改帧图像以移动。

希望这会有所帮助。

+0

完美运作..谢谢!但如果我将图像转移到中心,它将被消息阻挡..我怎么能这样做,图像在中心,然后下面的图像将是消息? – summer 2010-02-24 04:50:41

+0

关于这个你可以做的唯一的事情就是为消息添加另一个标签,并在标题末尾附加\ n \ n \ n ......这是我知道哪些不使用非文档化功能的唯一方法。 – 2010-02-24 07:20:45

+1

如果您将图像添加到UIAlertView,苹果是否会批准该应用程序? – Illep 2012-01-23 01:41:10

5

你需要子类UIAlertView并重新排列它的子视图。有几个教程这种东西:

+1

但我认为重新排列子视图可能导致拒绝的应用程式。 – 2010-02-24 08:36:17

+0

第一个链接已损坏。 – 2013-04-28 17:46:43

0

我对Madhup提供的解决方案做了一些修改。

从Madhup的解决方案工作罚款短消息,但是,当消息太长, 消息将被图像覆盖。

因此,我增加了以下步骤UIAlertViewDelegate 的方法 - (无效)willPresentAlertView:(UIAlertView中*)alertView

  1. 添加8 “\ n” 个作为消息的前缀,以向下推动消息, 预留图像空间(我的图像被限制在100x150)

  2. 检测AlertView的子视图,以确定UITextView是否存在。

    只有当消息太长时,UITextView才会存在。

  3. 如果UITextView不存在,一切都会好的,图片 显示不错,消息显示不错。

  4. 如果UITextView的存在,从UITextView.text, 取出8前缀“\ n”个,然后调用UITextView.setFrame以调整和改变的UITextView的位置。

上述操作正常。

我发送一个NSDictionary作为要显示的消息,该字典包含2个键值对,“msg”=>真实消息字符串。 “url”=>作为来自网站的图像。

使用NSURLConnection sendSynchronousRequest方法,代码将在途中从互联网上检索图像数据。

- (void)showAlertView:(NSDictionary *)msgDic { 
    NSLog(@"msgDic = %@", msgDic); 
    NSMutableString *msg = [[NSMutableString alloc] initWithString:@"\n\n\n\n\n\n\n\n"]; 
    if ([msgDic objectForKey:@"msg"]) { 
     [msg appendFormat:@"%@", [msgDic objectForKey:@"msg"]]; 
    } 
    else { 
     [msg setString:[msgDic objectForKey:@"msg"]]; 
    } 

    NSLog(@"msg = %@", msg); 
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Alert Title" 
               message:msg 
               delegate:self 
            cancelButtonTitle:@"Close" otherButtonTitles:nil]; 

    if ([msgDic objectForKey:@"url"]) { 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[msgDic objectForKey:@"url"]]]; 
     [request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData]; 

     NSData *imgData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
     if (imgData) { 
      UIImage *shownImage = [UIImage imageWithData:imgData]; 
      UIImageView *imgView = [[UIImageView alloc] initWithImage:shownImage]; 
      [imgView setFrame:CGRectMake(floor(284-100)/2.0, 47, 100, 150)]; 

      [alert addSubview:imgView]; 
      [imgView release]; 
     } 
    } 

    alert.delegate = self; 
    [alert show]; 
    [alert release]; 
    [msgDic release]; 
} 

- (void)willPresentAlertView:(UIAlertView *)alertView { 
int viewCount = [alertView.subviews count]; 

    NSLog(@"subviews count = %i", viewCount); 

    if (viewCount > 0) { 
     BOOL bFoundTextView = NO; 
     for (int count=0; count<=[alertView.subviews count] -1; count++) { 
      BOOL bIsTextView = NO; 
      UIView *subView = [alertView.subviews objectAtIndex:count]; 
      NSLog(@"view index %i classname = %@", count, [[subView class] description]); 

      bIsTextView = [[[subView class] description] isEqualToString:@"UIAlertTextView"]; 
      bFoundTextView |= bIsTextView; 

      if (bIsTextView) { 
       UITextView *textView = (UITextView *)subView; 
       NSMutableString *msg = [[NSMutableString alloc] initWithString:textView.text]; 
       [msg setString:[msg substringFromIndex:8]]; 
       textView.text = msg; 

       CGRect frame = textView.frame; 
       if (frame.origin.y != 205) { 
        frame.origin.y = 205; 
        frame.size.height -= 155; 
        [textView setFrame:frame]; 
       } 

       [msg release]; 
      } 
     }   
    } 
} 
5
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"your Title" message:@"Your Message" delegate:nil cancelButtonTitle:@"Your Title" otherButtonTitles:nil]; 

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 40, 40)]; 

NSString *loc = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Your Image Name"]]; 
UIImage *img = [[UIImage alloc] initWithContentsOfFile:loc]; 
[image setImage:img]; 

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { 
    [Alert setValue:image forKey:@"accessoryView"]; 
}else{ 
    [Alert addSubview:image]; 
} 

[Alert show]; 
+0

http://stackoverflow.com/questions/18729220/uialertview-addsubview-in-ios7 – user3182143 2016-01-21 09:46:50

+0

[alertView setValue:imageView forKey:@ “accessoryView的”]; – user3182143 2016-01-21 09:47:43

+0

[action setValue:accessoryImage forKey:@“image”]; – user3182143 2016-01-21 09:50:00

0

注为IOS 7和上述

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { 
    [alert setValue:imageView forKey:@"accessoryView"]; 
}else{ 
    [alert addSubview:imageView]; 
} 
9

在IOS 7或更高时,使用该代码

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 282)]; 
UIImage *wonImage = [UIImage imageNamed:@"iberrys.png"]; 
imageView.contentMode=UIViewContentModeCenter; 
[imageView setImage:wonImage]; 
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Arirang List" 
                message:@"phiên bản: 1.0\n website: www.iberrys.com\n email: [email protected]\nmobile: 0918 956 456" 
                delegate:self 
              cancelButtonTitle:@"Đồng ý" 
              otherButtonTitles: nil]; 
//check if os version is 7 or above 
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { 
    [alertView setValue:imageView forKey:@"accessoryView"]; 
}else{ 
    [alertView addSubview:imageView]; 
} 
[alertView show]; 
+0

@QuangMing你的代码调用UIAlertview 2次。而我不明白为什么? – Muju 2016-11-08 08:29:46

0

夫特版本:

let alertView = UIAlertView(title: "Alert", message: "Alert + Image", delegate: nil, cancelButtonTitle: "OK") 
    let imvImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) 
    imvImage.contentMode = UIViewContentMode.Center 
    imvImage.image = UIImage(named: "image_name") 
    alertView.setValue(imvImage, forKey: "accessoryView") 
    alertView.show() 
+0

如何调整警报视图或附件视图的大小?如果使用上面的代码,默认情况下会显示完整尺寸为 – AAA 2015-12-28 10:20:25

+0

的警报,但它们不提供任何方式对其进行自定义。如果你想要更多的自定义,你应该创建一个自定义提醒控制器。 (它是uiviewcontroller的一个子类) – 2015-12-28 10:55:56

0

使用纯布局荚:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hello" 
                     message:nil 
                    preferredStyle:UIAlertControllerStyleAlert]; 
UIImage *image = // target image here; 
CGSize size = image.size; 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, topMargin, size.width, size.height)]; 
imageView.image = image; 
[alertController.view addSubview:imageView]; 
[imageView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:leftMargin]; 
[imageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:topMargin]; 
[imageView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:rightMargin]; 
[imageView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:bottomMargin]; 
[imageView autoSetDimension:ALDimensionWidth toSize:size.width]; 
[imageView autoSetDimension:ALDimensionHeight toSize:size.height]; 
// add desired actions here 
[self presentViewController:alertController animated:YES completion:nil]; 
2
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 282)]; 
UIImage *wonImage = [UIImage imageNamed:@"iberrys.png"]; 
imageView.contentMode = UIViewContentModeCenter; 
[imageView setImage:wonImage]; 
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Arirang List" 
                message:@"phiên bản: 1.0\n website: www.iberrys.com\n email: [email protected]\nmobile: 0918 956 456" 
                delegate:self 
              cancelButtonTitle:@"Đồng ý" 
              otherButtonTitles:nil]; 
//check if os version is 7 or above 
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { 
    [alertView setValue:imageView forKey:@"accessoryView"]; 
} else { 
    [alertView addSubview:imageView]; 
} 
[alertView show]; 
相关问题