美文网首页
iOS总结:UIAlertController 弹窗显示图片

iOS总结:UIAlertController 弹窗显示图片

作者: SoaringHeart | 来源:发表于2020-03-07 17:23 被阅读0次

    需求:弹窗上添加一张图片
    想来想去还是 UIAlertController 实现最简单,方法是用message扩展弹窗高度,图片高度可调整

    WX20200307-171446@2x.png
    🌰🌰:           
    UIAlertController.showAlertImage(kMerchantNameTip, image: "img_merchant_name.png", actionTitles: [kTitleSure])
    
    //封装
    @objc public extension UIAlertController{
        /// 创建包含图片不含message的提示框
        static func createAlertImage(_ title: String?,
                                     image: String,
                                     contentMode: UIView.ContentMode = .scaleAspectFit,
                                      count: Int = 10,
                                      actionTitles: [String]? = [kTitleCancell, kTitleSure],
                                      handler: ((UIAlertController, UIAlertAction) -> Void)? = nil) -> UIAlertController {
            assert(UIImage(named: image) != nil)
            
            let msg = String(repeating: "\n", count: count)
            let alertController = UIAlertController(title: title, message: msg, preferredStyle: .alert)
            // 配置图片
            let image = UIImage(named: image)
            let imageView = UIImageView(image: image)
            imageView.contentMode = contentMode
            alertController.view.addSubview(imageView)
    
            imageView.translatesAutoresizingMaskIntoConstraints = false
            alertController.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: alertController.view, attribute: .centerX, multiplier: 1, constant: 0))
            alertController.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: alertController.view, attribute: .centerY, multiplier: 1, constant: 15))
            alertController.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 240))
            alertController.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 17*CGFloat(count) - 30))
            // 配置按钮
            actionTitles?.forEach({ (title:String) in
                let style: UIAlertAction.Style = [kTitleCancell, kTitleNo].contains(title) ? .destructive : .default
                alertController.addAction(UIAlertAction(title: title, style: style, handler: { (action: UIAlertAction) in
                    if handler != nil {
                        handler!(alertController, action)
                    }
                }))
            })
            return alertController
        }
    
        /// 创建包含图片不含message的提示框(count数字越大,弹窗高度越大)
        static func showAlertImage(_ title: String?,
                                     image: String,
                                     contentMode: UIView.ContentMode = .scaleAspectFit,
                                      count: Int = 10,
                                      actionTitles: [String]? = [kTitleCancell, kTitleSure],
                                      handler: ((UIAlertController, UIAlertAction) -> Void)? = nil){
            DispatchQueue.main.async {
                let alertVC = UIAlertController.createAlertImage(title, image: image, contentMode: contentMode, count: count, actionTitles: actionTitles, handler: handler)
                if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
                    rootVC.present(alertVC, animated: true, completion: nil)
                }
            }
        }
    }

    相关文章

      网友评论

          本文标题:iOS总结:UIAlertController 弹窗显示图片

          本文链接:https://www.haomeiwen.com/subject/szzcdhtx.html