ios swift alert 自定义弹框 点击半透明部分弹框消失

文章目录

1.BaseAlertVC

swift 复制代码
import UIKit

class BaseAlertVC: GLBaseViewController {
    
    let centerView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0.3)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapView))
        tap.delegate = self
        view.addGestureRecognizer(tap)
        
        view.addSubview(centerView)
    }
    
    @objc func tapView() {
        dismiss()
    }
    
    
}



extension BaseAlertVC: UIGestureRecognizerDelegate {


    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        
        if (touch.view?.isDescendant(of: centerView))! {
            return false;
        }else{
            return true;
        }
    }

}

2.BindFrameNumAlertVC

swift 复制代码
import UIKit
import XHToastSwift

class BindFrameNumAlertVC: BaseAlertVC,UITextFieldDelegate {
    
    let textField = UITextField()
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        // Get the new text after the user enters or deletes characters
        let newText = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? ""

        // Limit the text field to 15 characters
        return newText.count <= 15
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        
        centerView.backgroundColor = .white
        centerView.snp.makeConstraints { make in
            make.width.equalTo(300)
            make.height.equalTo(250)
            make.center.equalToSuperview()
        }
        
        centerView.layer.cornerRadius = 10
        
        let titleLabel = UILabel()
        centerView.addSubview(titleLabel)
        titleLabel.text = LocalizableManager.localValue("binding_frame_number")
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
        }
        
        textField.delegate = self
        textField.backgroundColor = UIColor.rgbColor(red: 219, green: 219, blue: 219)
        textField.keyboardType = .numberPad
        textField.textAlignment = .center
        centerView.addSubview(textField)
        textField.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(220)
            make.height.equalTo(50)
            make.top.equalTo(titleLabel.snp_bottom).offset(30)
        }
        textField.layer.cornerRadius = 10
        //限制textField只能输入15个字符
        
        
        let alertLabel = UILabel()
        alertLabel.numberOfLines = 0
        alertLabel.font = UIFont(name: "PingFang SC", size: 13)
        alertLabel.textColor = .gray
        alertLabel.text = LocalizableManager.localValue("input_corresponding_code")
        centerView.addSubview(alertLabel)
        alertLabel.snp.makeConstraints { make in
            make.top.equalTo(textField.snp_bottom).offset(10)
            make.left.equalTo(textField).offset(10)
            make.right.equalTo(textField)
        }
        
        
        let sureBtn = UIButton()
        sureBtn.backgroundColor = UIColor.rgbColor(red: 219, green: 219, blue: 219)
        sureBtn.setTitleColor(.black, for: .normal)
        sureBtn.addTarget(self, action: #selector(sureBtnClick), for: .touchUpInside)
        sureBtn.setTitle(LocalizableManager.localValue("sure"), for: .normal)
        centerView.addSubview(sureBtn)
        sureBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(30)
            make.bottom.equalToSuperview().offset(-20)
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
        sureBtn.layer.cornerRadius = 20
        
        let cancelBtn = UIButton()
        cancelBtn.backgroundColor = newUIYellow
        cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
        cancelBtn.setTitle(LocalizableManager.localValue("cancel"), for: .normal)
        centerView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-30)
            make.bottom.equalToSuperview().offset(-20)
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
        cancelBtn.layer.cornerRadius = 20
        
    }
    

    @objc func sureBtnClick(){
        print(textField.text)
        guard let str = textField.text,str.count == 15 else {
            XHToast.showBottomWithText(LocalizableManager.localValue("input_only_six_num"))
            return
        }
      
       
    }

    @objc func cancelBtnClick(){
       
    }
    

}
相关推荐
職場上的造物主8 小时前
高清种子资源获取指南 | ✈️@seedlinkbot
python·ios·php·音视频·视频编解码·视频
Kevin Coding11 小时前
Flutter使用Flavor实现切换环境和多渠道打包
android·flutter·ios
Bruce_Liuxiaowei2 天前
基于阿里云百炼大模型Sensevoice-1的语音识别与文本保存工具开发
人工智能·阿里云·语音识别·xcode
wn5312 天前
【浏览器 - Mac实时调试iOS手机浏览器页面】
前端·macos·ios·智能手机·浏览器
小鹿撞出了脑震荡3 天前
Effective Objective-C 2.0 读书笔记—— 方法调配(method swizzling)
ios·objective-c·swift
小鹿撞出了脑震荡3 天前
Effective Objective-C 2.0 读书笔记—— 消息转发
ios·objective-c·swift
一丝晨光4 天前
Cocoa和Cocoa Touch是什么语言写成的?什么是Cocoa?编程语言中什么是框架?为什么苹果公司Cocoa类库有不少NS前缀?Swift编程语言?
macos·ios·objective-c·cocoa·swift·uikit·cocoa touch
{⌐■_■}6 天前
【Validator】自定义字段、结构体补充及自定义验证,go案例讲解ReportError和errors.As在其中的使用
开发语言·golang·xcode
LucianaiB7 天前
字节iOS面试经验分享:HTTP与网络编程
网络·ios·面试
Swift社区7 天前
LeetCode - #195 Swift 实现打印文件中的第十行
vue.js·leetcode·swift