【纯代码】Swift - 自定义底部弹窗基类(可根据需要自行扩展内容)
//弹窗视图 class PopView : UIView { var selectButtonCallBack:((_ title:String)-> Void)? var contenView:UIView? { didSet{ setUpContent() } } override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setUpContent(){ if self.contenView != nil { self.contenView?.frame.origin.y = UIScreen.main.bounds.size.height - 191 self.addSubview(self.contenView!) } self.backgroundColor = newColorWithAlpha(0, 0, 0, 0.4) self.isUserInteractionEnabled = true self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(dismissView))) //以下为添加内容,可根据需要删除以下部分 sudokuConstraints() } @objc func dismissView(){ UIView.animate(withDuration: 0.3, animations: { self.alpha = 0 }) { (true) in self.removeFromSuperview() self.contenView?.removeFromSuperview() } } func showInWindow(){ UIApplication.shared.keyWindow?.addSubview(self) UIView.animate(withDuration: 0.3, animations: { self.alpha = 1.0 self.contenView?.frame.origin.y = UIScreen.main.bounds.size.height - 191 }, completion: nil) } //MARK: - 布局 func sudokuConstraints() -> Void { let titleArr = ["京","沪","浙","苏","粤","鲁","晋","翼", "豫","川","渝","辽","吉","黑","皖","鄂", "湘","赣","闽","陕","甘","宁","蒙","津", "贵","云","桂","琼","青","新","藏"] for (index,value) in titleArr.enumerated() { let button = createButton(title: value) let margin = (UIScreen.main.bounds.size.width - 8 * 39)/(8 + 1) let col = CGFloat(index % Int(8)) let row = CGFloat(index / Int(8)) let viewX = margin + col * (39 + margin) let viewY = 7 + row * (39 + 7) button.frame = CGRect(x: viewX, y: viewY, width: 39, height: 39) self.contenView!.addSubview(button) } } func createButton(title:String) -> UIButton { let button = UIButton() button.setTitle(title, for: .normal) button.setTitleColor(newColor(0, 0, 0), for: .normal) button.backgroundColor = .white button.layer.masksToBounds = true button.layer.cornerRadius = 5.0 button.addTarget(self, action: #selector(buttonClickAction(button:)), for: .touchUpInside) return button } @objc func buttonClickAction(button:UIButton) -> Void { if self.selectButtonCallBack != nil { self.selectButtonCallBack!(button.titleLabel?.text ?? "粤") } } }
使用:
let popview = PopView.init(frame:UIScreen.main.bounds) popview.contenView = UIView.init(frame: CGRect.init(x: 0, y: UIScreen.main.bounds.size.height - 191 , width: UIScreen.main.bounds.size.width, height:191 )) popview.contenView?.backgroundColor = newColor(206, 206, 206) popview.selectButtonCallBack = { (title:String) -> Void in self.righAbbreviationButton.setTitle(title, for: .normal) popview.dismissView() } popview.showInWindow()
效果图:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期