【问题标题】:Is it possible to show an Image in UIAlertView?是否可以在 UIAlertView 中显示图像?
【发布时间】:2011-01-20 09:09:09
【问题描述】:

是否可以在 UIAlertView 中添加图像,例如显示 plist 文件中的图像?

【问题讨论】:

    标签: objective-c iphone uialertview


    【解决方案1】:

    你可以这样做:

    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];
    

    这将在您的警报视图的右上角添加一个图像,您可以更改框架图像以四处移动。

    希望这会有所帮助。

    【讨论】:

    • 完美运行..谢谢!但是如果我将图像移到中心,它将被消息阻止..我怎样才能以图像位于中心的方式做到这一点,然后图像下方将是消息?
    • 您唯一能做的就是为消息添加另一个标签并在标题末尾附加 \n\n\n... 这是我知道的唯一方法使用未记录的功能。
    • 如果你向 UIAlertView 添加图片,苹果会批准申请吗?
    • 对于从谷歌来到这里的任何人,这不再适用于 iOS7。但是,您可以查看this,它是具有图像支持的 UIAlertView 的副本。
    • stackoverflow.com/a/22861794/17239 是 iOS 7 的一个简短而有效的扩展。
    【解决方案2】:

    在 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: quangminh@berrys.com\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];
    

    【讨论】:

    • @QuangMing 你的代码调用了 UIAlertview 2 次。我不明白为什么?
    【解决方案3】:

    您需要继承 UIAlertView 并重新排列其子视图。这种东西有几个教程:

    【讨论】:

    • 但我认为重新排列子视图可能会导致应用被拒绝。
    【解决方案4】:
    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];
    

    【讨论】:

    • [alertView setValue:imageView forKey:@"accessoryView"];
    • [action setValue:accessoryImage forKey:@"image"];
    【解决方案5】:
    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: quangminh@berrys.com\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];
    

    【讨论】:

      【解决方案6】:

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

      Madhup 的解决方案适用于短消息,但是, 消息过长时,会被图片覆盖。

      因此,我在 UIAlertViewDelegate 的方法中添加了以下步骤 - (void)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];
                  }
              }        
          }
      }
      

      【讨论】:

        【解决方案7】:

        IOS 7 及更高版本的注意事项

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

        【讨论】:

          【解决方案8】:

          Swift 版本:

              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()
          

          【讨论】:

          • 如何调整警报视图或附件视图的大小?如果我使用上面的代码,警报会显示为全尺寸
          • 默认情况下,他们不提供任何自定义方式。如果您想要更多自定义,您应该创建一个自定义警报控制器。 (它是uiviewcontroller的子类)
          【解决方案9】:

          使用纯布局 pod:

          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];
          

          【讨论】:

          • “UIImageView”没有可见的@interface 声明选择器“autoPinEdgeToSuperviewEdge:withInset:”
          • 我特意说是用纯布局oid做的
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多