【问题标题】:Adding an image to a UIAlertController将图像添加到 UIAlertController
【发布时间】:2015-09-26 01:22:36
【问题描述】:

这是我的警报,效果很好。我想向警报中添加一个图像,当警报出现时,它会在文本旁边显示,如果它有所作为,我会在 SpriteKit 中的 SKScene 中。

var alertController = UIAlertController(title: "How to Skate", message: "Tap the screen to perform a trick and jump over the obsticles (You can grind on rails) The game will end when you hit a highlighted red, orange or yellow obstacle. That's it! + Image", preferredStyle: UIAlertControllerStyle.Alert)     
alertController.addAction(UIAlertAction(title: "Cool!", style: UIAlertActionStyle.Cancel, handler: nil))
self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)

【问题讨论】:

    标签: ios swift uialertcontroller


    【解决方案1】:

    您可以将UIImageView 作为子视图添加到您的UIAlertController

    var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40))
    imageView.image = yourImage
    alert.view.addSubview(imageView)
    

    这就是你在UIAlertController中的做法:

    let alertMessage = UIAlertController(title: "My Title", message: "My Message", preferredStyle: .Alert)
    
    let image = UIImage(named: "myImage")
    var action = UIAlertAction(title: "OK", style: .Default, handler: nil)
    action.setValue(image, forKey: "image")
    alertMessage .addAction(action)
    
    self.presentViewController(alertMessage, animated: true, completion: nil)
    

    【讨论】:

    • 这些方法不合规。它有效地将图像添加到按钮区域并影响警报视图中其他按钮的大小。这种方法的可访问性也可能不兼容。 Apple 应该可以开箱即用地执行此操作,但如果没有该选项,由 UIView 生成的自定义警报似乎是最好的方法,并且具有控制所有元素的能力。
    【解决方案2】:
        let alertMessage = UIAlertController(title: "My Title", message: "", preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)
        action.setValue(UIImage(named: "task1.png")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
        alertMessage .addAction(action)
        self.present(alertMessage, animated: true, completion: nil)
    

    斯威夫特 3

    【讨论】:

      【解决方案3】:

      你可以这样做。

          let imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40))
         // imageView.image = UIImage(named: "ic_no_data")
          let alertMessage = UIAlertController(title: "My Title", message: "", preferredStyle: .Alert)
      
          let image = UIImage(named: "Image")
          let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
          action.setValue(image, forKey: "image")
          alertMessage .addAction(action)
      
          self.presentViewController(alertMessage, animated: true, completion: nil)
          alertMessage.view.addSubview(imageView)
      

      【讨论】:

        【解决方案4】:

        如果您愿意使用私有 API,您可以使用私有 attributedMessage 属性将属性字符串设置为包含图像的消息:

        来源:https://github.com/JaviSoto/iOS10-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIAlertController.h

        let actionSheet = UIAlertController(title: "title", message: nil, preferredStyle: .actionSheet)
        
        let str = NSMutableAttributedString(string: "Message\n\n", attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption1), NSAttributedStringKey.foregroundColor: UIColor.gray])
        
        let attachment = NSTextAttachment()
        
        attachment.image = --> yourImage <--
        
        str.append(NSAttributedString(attachment: attachment))
        
        actionSheet.setValue(str, forKey: "_attributedMessage")
        

        同样,这是一个私有 API,因此可能会在任何版本中更改,恕不另行通知。慎用!

        【讨论】:

          【解决方案5】:

          SWIFT 4 +

                  let refreshAlert = UIAlertController(title: "Unlike Article", message: "Are you sure you want to unlike this Article?", preferredStyle: UIAlertControllerStyle.alert)
          
          
          
                  let cancel = UIAlertAction(title:  "CANCEL", style: .default, handler: { (action: UIAlertAction!) in
          
          
          
                      return
                  })
          
                  let image = #imageLiteral(resourceName: "dislike_icon").resizedImage(newSize: CGSize(width: 25, height: 25))
                  let unLike = UIAlertAction(title: "UNLIKE", style: .destructive, handler: { (action: UIAlertAction!) in
                      self.articleUnLiking()
          
          
                      return
                  })
                  unLike.setValue(image.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
                  refreshAlert.addAction(cancel)
                  refreshAlert.addAction(unLike)
                  self.present(refreshAlert, animated: true, completion: nil)
          

          注意:- 同样,这是一个私有 API,因此可能会在任何版本中更改,恕不另行通知。慎用!

          谢谢

          【讨论】:

            【解决方案6】:

            向 UIAlertController 添加图像

            请尝试以下代码 Swift 4 +

            1. 添加alertView控制器

              let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert) 让 image = UIImage(named:"feet")! alert.addImage(图像:图像) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) // 显示警报 self.present(alert, 动画: true, 完成: nil)

            2. 添加扩展 UIAlertController

              func addImage(image: UIImage) {

              let maxSize = CGSize(width: 240, height: 244)
              let imgSize = image.size
              var ratio:CGFloat!
              if (imgSize.width > imgSize.height){
                  ratio = maxSize.width / imgSize.width
              }else {
                  ratio = maxSize.height / imgSize.height
              }
              let scaleSize = CGSize(width: imgSize.width*ratio, height: imgSize.height*ratio)
              var resizedImage = image.imageWithSize(scaleSize)
              
              if (imgSize.height > imgSize.width) {
                  let left = (maxSize.width - resizedImage.size.width) / 2
                  resizedImage = resizedImage.withAlignmentRectInsets(UIEdgeInsetsMake(0, -left, 0, 0))
              }
              
              let imgAction = UIAlertAction(title: "", style: .default, handler: nil)
              imgAction.isEnabled = false
              imgAction.setValue(resizedImage.withRenderingMode(.alwaysOriginal), forKey: "image")
              
              self.addAction(imgAction)
              

              }

            添加图像视图扩展

            扩展 UIImage {

            func imageWithSize(_ size:CGSize) -> UIImage {
                var scaledImageRect = CGRect.zero
            
                let aspectWidth:CGFloat = size.width / self.size.width
                let aspectHeight:CGFloat = size.height / self.size.height
                let aspectRatio:CGFloat = min(aspectWidth, aspectHeight)
            
                scaledImageRect.size.width = self.size.width * aspectRatio
                scaledImageRect.size.height = self.size.height * aspectRatio
                scaledImageRect.origin.x = (size.width - scaledImageRect.size.width) / 2.0
                scaledImageRect.origin.y = (size.height - scaledImageRect.size.height) / 2.0
            
                UIGraphicsBeginImageContextWithOptions(size, false, 0)
            
                self.draw(in: scaledImageRect)
            
                let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
            
                return scaledImage!
            }
            

            }

            【讨论】:

              【解决方案7】:

              目前,在不使用私有 api 或其他黑客的情况下,使用 UIAlertController 并没有真正的正确方法,因为根据文档,您不应该修改警报视图控制器的视图层次结构。所以最好的解决方案是为此创建一个自定义视图。但是,如果您不关心一些额外的边框,那么在 UITextField 中使用属性字符串可以:

              alertController.addTextField { field in
                  let attachment = NSTextAttachment()
                  attachment.image = image
                  attachment.bounds = CGRect(x: 0, y: 0, width: 226, height: 226)
                  field.attributedPlaceholder = NSAttributedString(attachment: attachment)
                  field.textAlignment = .center
                  field.isEnabled = false
              }
              

              【讨论】:

                猜你喜欢
                • 2014-12-08
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-08-17
                • 2015-12-23
                • 2018-05-17
                • 1970-01-01
                • 2015-10-19
                相关资源
                最近更新 更多