UIAlertController + Extension 自定义UIView

demo地址

UIAlertController + Extension 自定义UIView_第1张图片
使用说明

//
//  ViewController.swift
//  Alert_Dialog
//
//  Created by Sico2Sico on 02/05/2018.
//  Copyright (c) 2018 Sico2Sico. All rights reserved.
//

import UIKit
import Alert_Dialog
import ReactiveSwift
import ReactiveCocoa
import Result

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        let newView = UIControl()
        newView.backgroundColor = UIColor.red

        let alret  = UIAlertController(customView: newView) { (make) in
            make.size.equalTo(300)
            make.center.equalToSuperview()
        }

        newView.reactive
            .controlEvents(UIControlEvents.touchUpInside)
            .observeValues {[weak alret] (view) in
                print("处理问题")
                alret!.dismiss(animated:true, completion:nil)
        }

        alret.show()

    }


}

源文件

//
//  UIAlertController+Dialog.swift
//  AppUIKit
//
//  Created by 德志 on 2018/2/5.
//

import UIKit
import SnapKit

extension UIAlertController {

    public convenience init(customView : UIView, closure:@escaping (_ make: ConstraintMaker) -> Void){
        self.init(title:nil, message:nil, preferredStyle: UIAlertControllerStyle.alert)
        view.clipsToBounds = false

        customView.tag = 1111
        view.addSubview(customView)

        // 这里需要加一个action 不然会报错
        let action = UIAlertAction(title:"", style: .default, handler:nil)
        addAction(action)

        //约束
        customView.snp.makeConstraints { (make) in
            closure(make)
        }

        // 隐藏系统的View
        _ = self.view.subviews.flatMap { (view) -> Void in
            if view.tag != 1111 {
                view.isHidden = true
            }else {
                view.isHidden = false
            }
        }
    }



    public func show(animated: Bool = true, style: UIBlurEffectStyle? = nil, completion: (() -> Void)? = nil) {

        if let style = style {
            for subview in view.allSubViewsOf(type: UIVisualEffectView.self) {
                subview.effect = UIBlurEffect(style: style)
            }
        }

        DispatchQueue.main.async {
            UIApplication.shared.keyWindow?.rootViewController?.present(self, animated: animated, completion: completion)
        }
    }

}

你可能感兴趣的:(iOS,Swift,UIAlert,Extension,扩展,Swift)