当前位置: 首页 > news >正文

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

文章目录

  • 1.BaseAlertVC
  • 2.BindFrameNumAlertVC

请添加图片描述

1.BaseAlertVC

import UIKitclass 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 = selfview.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

import UIKit
import XHToastSwiftclass 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 characterslet newText = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? ""// Limit the text field to 15 charactersreturn newText.count <= 15}override func viewDidLoad() {super.viewDidLoad()centerView.backgroundColor = .whitecenterView.snp.makeConstraints { make inmake.width.equalTo(300)make.height.equalTo(250)make.center.equalToSuperview()}centerView.layer.cornerRadius = 10let titleLabel = UILabel()centerView.addSubview(titleLabel)titleLabel.text = LocalizableManager.localValue("binding_frame_number")titleLabel.snp.makeConstraints { make inmake.centerX.equalToSuperview()make.top.equalToSuperview().offset(20)}textField.delegate = selftextField.backgroundColor = UIColor.rgbColor(red: 219, green: 219, blue: 219)textField.keyboardType = .numberPadtextField.textAlignment = .centercenterView.addSubview(textField)textField.snp.makeConstraints { make inmake.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 = 0alertLabel.font = UIFont(name: "PingFang SC", size: 13)alertLabel.textColor = .grayalertLabel.text = LocalizableManager.localValue("input_corresponding_code")centerView.addSubview(alertLabel)alertLabel.snp.makeConstraints { make inmake.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 inmake.left.equalToSuperview().offset(30)make.bottom.equalToSuperview().offset(-20)make.width.equalTo(100)make.height.equalTo(40)}sureBtn.layer.cornerRadius = 20let cancelBtn = UIButton()cancelBtn.backgroundColor = newUIYellowcancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)cancelBtn.setTitle(LocalizableManager.localValue("cancel"), for: .normal)centerView.addSubview(cancelBtn)cancelBtn.snp.makeConstraints { make inmake.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(){}}
http://www.lryc.cn/news/118042.html

相关文章:

  • HCIP STP(生成树)
  • 【Unity开发必备】100多个 Unity 学习网址 资源 收藏整理大全【持续更新】
  • Alpine Ridge控制器使其具备多种使用模式 - 英特尔发布雷电3接口:竟和USB Type-C统一了
  • 容器——2.Collection 子接口之 List
  • 【工作记录】docker安装gitlab、重置密码@20230809
  • 数据挖掘的基本概念和大数据的特点
  • LabVIEW开发分段反射器测试台
  • 二级python和二级c哪个简单,二级c语言和二级python
  • E: Package ‘curl‘ has no installation candidate/ E:软件包没有可用的安装源
  • 代理模式及常见的3种代理类型对比
  • 8.6 校招 内推 面经
  • 【大数据之Flume】七、Flume进阶之自定义Sink
  • vue对于时间的处理
  • Apache DolphinScheduler 3.1.8 版本发布,修复 SeaTunnel 相关 Bug
  • 科技云报道:一波未平一波又起?AI大模型再出邪恶攻击工具
  • 深度对话|如何设计合适的网络经济激励措施
  • opencv带GStreamer之Windows编译
  • Java并发编程之锁的升级
  • 多核异构处理器A核与M核通信过程
  • 面试热题(反转链表)
  • 竞赛项目 深度学习的水果识别 opencv python
  • Java项目部署云windows细节
  • 软件功能测试有什么注意事项?功能测试报告起到什么作用?
  • Kubernetes 调度 约束
  • Grafana技术文档-概念-《十分钟扫盲》
  • 【JavaEE进阶】Spring 更简单的读取和存储对象
  • KafKa集群搭建和知识点
  • 剑指 Offer 56 - I. 数组中数字出现的次数题解
  • CSDN付费专栏写作协议
  • [保研/考研机试] KY30 进制转换-大整数转二进制 清华大学复试上机题 C++实现