UIAlertController + Extension 自定义UIView

17 篇文章 0 订阅

demo地址

这里写图片描述
使用说明

//
//  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)
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值