iOS自定義彈窗Swift

擔(dān)心表述不清楚,直接上代碼:https://gitee.com/xgkp/necessaryAlert.git
以下是簡單流程:

  1. 創(chuàng)建一個(gè)UsefulAlertView繼承于UIview,得到一個(gè)UsefulAlertView.swift文件
    2.Xib創(chuàng)建一個(gè)同名的UsefulAlertView.xib
0.png

并且放置一個(gè)Button
3.綁定UsefulAlertView.swift 和 UsefulAlertView.xib
注意綁定控件的時(shí)候要注意選擇區(qū)域:如下圖


1.png

Button的視圖和UsefulAlertView.swift綁定
Button的點(diǎn)擊事件和UsefulAlertView.swift綁定

  1. UsefulAlertView.swift里的代碼初始化并設(shè)置點(diǎn)擊事件
//
//  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)
    }
    
    //初始化默認(rèn)屬性配置
    func initialSetup(){
        TestDisplayBtn.backgroundColor = UIColor.red
    }
    
    @IBAction func AlertBtnAction(_ sender: UIButton) {
        if buttonCallBack != nil {
            buttonCallBack!()
        }
    }
    }

5.Viewcontroller中的調(diào)用和點(diǎn)擊事件的賦值

//
//  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.具體按鈕點(diǎn)擊事件可以用block,target,delegate實(shí)現(xiàn),本文選擇用的是block,如果需要自己用target和delegate寫,請參考以下第二個(gè)鏈接。
參考來源:http://www.hangge.com/blog/cache/detail_1394.html
參考來源:http://blog.csdn.net/syg90178aw/article/details/47020097
參考來源:http://www.itdecent.cn/p/b8a46bc55373

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容