【问题标题】:Xcode Swift how to add image to UIAlertController options? [duplicate]Xcode Swift 如何将图像添加到 UIAlertController 选项? [复制]
【发布时间】:2017-12-19 10:36:59
【问题描述】:

我想将图像/图标添加到UIAlertController,就像苹果音乐播放器中的对话框一样

我想要如下图所示的彩色/大小图像/图标,而不是 this question 里面的那个。我相信它的 ios 11+ 功能,但我找不到它的文档。

就是这样:

我可以在UIAlertController 中执行此操作吗,或者我应该创建自己的自定义对话框uiviewcontroller

【问题讨论】:

  • @the4kman 它没有重复我在你的链接中看到了这个问题,有很小的图像,我想要的是相同大小的彩色图像:)
  • 答案也涉及彩色图像。
  • 你能告诉我答案选项中的彩色图像吗?
  • @the4kman 我将在您提供的链接选项中等待您对彩色图像的回答:) 谢谢
  • 嗨,您可以在警报中添加多个按钮您可以查看本教程blog.ijasoneverett.com/2016/04/…希望这有帮助

标签: swift xcode uialertcontroller


【解决方案1】:
 let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)            
 alert.modalPresentationStyle = .popover


 let image = UIImage(named: "logoCircle")
 let imageView = UIImageView()
 imageView.image = image
 imageView.frame =  CGRect(x: 25, y: 18, width: 24, height: 24)
 alert.view.addSubview(imageView) 

 let image1 = UIImage(named: "icshare")
  let imageView1 = UIImageView()
  imageView1.image = image1
  alert.view.addSubview(imageView1)
  imageView1.frame = CGRect(x: 25, y: 75, width: 24, height: 24)

 let shareExternal = UIAlertAction(title: NSLocalizedString("Share External Link", comment: ""), style: .default) { action in
        }
 let shareInApp = UIAlertAction(title: "Share within", style: .default)   {
                action in
            } 

  alert.addAction(shareInApp)
  alert.addAction(shareExternal)


    if let presenter = alert.popoverPresentationController
    {
            presenter.sourceView = button
            presenter.sourceRect = button.bounds
    }
    present(alert, animated: true, completion: nil)

【讨论】:

  • 所以这里的诀窍是我将图像添加到UIAlertController 视图中!它有效,但需要像上图一样在多个选项上进行,+1 表示想法
  • @杰克。检查我的更新答案
  • @UmaMadhavi 很好的答案!知道为什么这会导致 Swift 5 中的布局约束问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多