IOS

iOS自定义弹窗Swift

2018-01-18  本文已影响710人  数字d

担心表述不清楚,直接上代码:https://gitee.com/xgkp/necessaryAlert.git
以下是简单流程:

  1. 创建一个UsefulAlertView继承于UIview,得到一个UsefulAlertView.swift文件
    2.Xib创建一个同名的UsefulAlertView.xib
0.png

并且放置一个Button
3.绑定UsefulAlertView.swift 和 UsefulAlertView.xib
注意绑定控件的时候要注意选择区域:如下图


1.png

Button的视图和UsefulAlertView.swift绑定
Button的点击事件和UsefulAlertView.swift绑定

  1. UsefulAlertView.swift里的代码初始化并设置点击事件
//
//  UsefulAlertView.swift
//  newproject
//
//  Created by Pro on 2018/1/17.
//  Copyright © 2018年 Pro. All rights reserved.
//

import UIKit

class UsefulAlertView: UIView {

    @IBOutlet weak var TestDisplayBtn: UIButton!
    
    var ConnectView:UIView!
    
    var buttonCallBack:(() -> ())?


    override init(frame: CGRect) {
        super.init(frame: frame)
        ConnectView = loadViewFromNib()
        addSubview(ConnectView)
        addConstraints()
        initialSetup()
    }
    

    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        ConnectView = loadViewFromNib()
        addSubview(ConnectView)
        addConstraints()
        initialSetup()
    }
    
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        return view
    }

    func addConstraints() {
        ConnectView.translatesAutoresizingMaskIntoConstraints = false
        var constraint = NSLayoutConstraint(item: ConnectView, attribute: .leading,
                                            relatedBy: .equal, toItem: self, attribute: .leading,
                                            multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: ConnectView, attribute: .trailing,
                                        relatedBy: .equal, toItem: self, attribute: .trailing,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: ConnectView, attribute: .top, relatedBy: .equal,
                                        toItem: self, attribute: .top, multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: ConnectView, attribute: .bottom,
                                        relatedBy: .equal, toItem: self, attribute: .bottom,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
    }
    
    //初始化默认属性配置
    func initialSetup(){
        TestDisplayBtn.backgroundColor = UIColor.red
    }
    
    @IBAction func AlertBtnAction(_ sender: UIButton) {
        if buttonCallBack != nil {
            buttonCallBack!()
        }
    }
    }

5.Viewcontroller中的调用和点击事件的赋值

//
//  ViewController.swift
//  newproject
//
//  Created by Pro on 2018/1/17.
//  Copyright © 2018年 Pro. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    override func viewDidAppear(_ animated: Bool){
        super.viewDidAppear(animated)
        
        let myAlert = UsefulAlertView(frame: CGRect(x: 0 , y: 0, width: UIScreen.main.bounds.width , height: UIScreen.main.bounds.height))
                myAlert.buttonCallBack = {() -> () in
                    myAlert.removeFromSuperview()
                    print("click it")
                    
        }

        self.view.addSubview(myAlert)
        
       
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}


6.具体按钮点击事件可以用block,target,delegate实现,本文选择用的是block,如果需要自己用target和delegate写,请参考以下第二个链接。
参考来源:http://www.hangge.com/blog/cache/detail_1394.html
参考来源:http://blog.csdn.net/syg90178aw/article/details/47020097
参考来源:https://www.jianshu.com/p/b8a46bc55373

上一篇 下一篇

猜你喜欢

热点阅读