iOS总结:UIAlertController 弹窗显示图片

2020-03-07  本文已影响0人  SoaringHeart

需求:弹窗上添加一张图片
想来想去还是 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)
            }
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读