Xcode Swift 如何将图像添加到 UIAlertController 选项? [英] Xcode Swift how to add image to UIAlertController options?

查看:36
本文介绍了Xcode Swift 如何将图像添加到 UIAlertController 选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想要的颜色/大小的图像/图标如下图所示,而不是里面的那个

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

解决方案

 let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)alert.modalPresentationStyle = .popoverlet image = UIImage(named: "logoCircle")让 imageView = UIImageView()imageView.image = 图像imageView.frame = CGRect(x: 25, y: 18, width: 24, height: 24)alert.view.addSubview(imageView)让 image1 = UIImage(named: "icshare")让 imageView1 = UIImageView()imageView1.image = image1alert.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}让 shareInApp = UIAlertAction(title: "Share inside", style: .default) {行动}alert.addAction(shareInApp)alert.addAction(shareExternal)如果让presenter = alert.popoverPresentationController{Presenter.sourceView = 按钮Presenter.sourceRect = button.bounds}存在(警报,动画:真实,完成:无)

i want to add image/icon to UIAlertController like the dialog inside apple music player

what i want colored/sized image/icon like the image below, not like the one inside this question .i believe its ios 11+ feature but i can't find the documents for it .

exactly like this :

can i do this in UIAlertController or i should make my own custom dialog uiviewcontroller ?

解决方案

 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)

这篇关于Xcode Swift 如何将图像添加到 UIAlertController 选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆