网上找了些例子,但是并不能满足我的要求,下面我将网上的荔枝与自己的改进分享给大家

原理: 通过swift4的present 结合SnapKit进行布局

看效果图:

1、包含自定义view:

2:显示List

项目引进Snapkit

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'target 'Test' do# Comment the next line if you don't want to use dynamic frameworksuse_frameworks!pod 'SnapKit'# Pods for lhcp
end

弹出框的基类: 可继承实现自己的效果

import SnapKit
import UIKit
class BasePopView: UIViewController{var contentVeiw = UIView()var viewHight: CGFloat = 150var needClose = truelet kScreenWidth: CGFloat = UIScreen.main.bounds.size.widthlet kScreenHeight: CGFloat = UIScreen.main.bounds.size.heightrequired init() {super.init(nibName: nil, bundle: nil)view.backgroundColor = UIColor.clearself.providesPresentationContextTransitionStyle = trueself.definesPresentationContext = trueself.modalPresentationStyle = .custom// 初始化UIsetupUIViews()}required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}override func viewWillAppear(_ animated: Bool) {super.viewWillAppear(animated)UIView.animate(withDuration: 0.25) {self.contentVeiw.alpha = 1}}func setupUIViews() {self.contentVeiw = UIView()self.contentVeiw.backgroundColor = UIColor.whiteview.addSubview(self.contentVeiw)self.contentVeiw.snp.makeConstraints { (make) -> Void inmake.left.equalTo(0)make.right.equalTo(0)make.width.equalTo(kScreenWidth)make.height.equalTo(viewHight)make.top.equalTo(self.view).offset(kScreenHeight-viewHight)}if(needClose){let close = UIButton()close.titleLabel?.font = UIFont.systemFont(ofSize: 12)close.setTitle("关闭", for: UIControl.State())close.setTitleColor(UIColor.black, for: UIControl.State())close.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.centerclose.backgroundColor = .whiteclose.addTarget(self, action: #selector(sheetViewDismiss), for: .touchUpInside)self.contentVeiw.addSubview(close)close.snp.makeConstraints { (make) -> Void inmake.right.equalTo(0)make.width.equalTo(35)make.height.equalTo(20)make.top.equalTo(self.contentVeiw)}}}@objc func sheetViewDismiss() {UIView.animate(withDuration: 0.1, animations: {self.contentVeiw.alpha = 0}) { (_) inself.dismiss(animated: false, completion: nil)}}override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {//如果所有点都在ContentView外 才关闭for touch:AnyObject in touches{let point = (touch as AnyObject).location(in: self.view)let pt = self.contentVeiw.layer.convert(point, from: self.view.layer)if self.contentVeiw.layer.contains(pt){return}}sheetViewDismiss()}@objc func cancelBtnDidClick(btn: UIButton) {sheetViewDismiss()}
}

List部分代码:

import UIKit
import SnapKit
class ListPopViewController: BasePopView {// ----------------外界调用的属性值--------------var valueBlock: valueBlock?/// cell内容var cellTitleList = [String]()/// cell字体颜色var cellTitleColor = UIColor.white/// cell字体大小var cellTitleFont: CGFloat = 16/// 标题var titleString: String? = "标题"/// 标题字体大小var titleFont: CGFloat = 15// ---------------内部属性-------------------typealias valueBlock = (NSInteger)->Swift.Voidvar tableView = UITableView()let cellID = "cellID"var tableViewHight: CGFloat {return CGFloat(cellTitleList.count) * rowHight + headerViewHight + footerViewHight}fileprivate let rowHight: CGFloat = 35fileprivate let headerViewHight: CGFloat = 35fileprivate var footerViewLineHight: CGFloat = 5fileprivate var footerViewHight: CGFloat {return headerViewHight + footerViewLineHight}required init?(cellTitleList: [String]!) {super.init()self.needClose = false// 初始化self.cellTitleList = cellTitleListself.viewHight = tableViewHightview.backgroundColor = UIColor.clearself.providesPresentationContextTransitionStyle = trueself.definesPresentationContext = trueself.modalPresentationStyle = .custom// 初始化UIsetupListViews()}required init() {fatalError("init() has not been implemented")}required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}override func viewDidLoad() {super.viewDidLoad()}func setupListViews() {tableView = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: tableViewHight), style: .plain)tableView.delegate = selftableView.dataSource = selftableView.showsVerticalScrollIndicator = falsetableView.showsHorizontalScrollIndicator = falsetableView.isScrollEnabled = falsetableView.bounces = falsetableView.isPagingEnabled = falsetableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)tableView.register(TitleCell.classForCoder(), forCellReuseIdentifier: cellID)view.addSubview(tableView)self.tableView.snp.makeConstraints { (make) -> Void inmake.left.equalTo(0)make.right.equalTo(0)make.height.equalTo(tableViewHight)make.top.equalTo(self.view).offset(kScreenHeight-tableViewHight)}}}// MARK: UITableViewDelegate, UITableViewDataSource
extension ListPopViewController: UITableViewDelegate, UITableViewDataSource {func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return cellTitleList.count}func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! TitleCellcell.titleLabel.text = cellTitleList[indexPath.row]cell.titleLabel.textColor = cellTitleColorcell.titleLabel.font = UIFont.systemFont(ofSize: cellTitleFont)return cell}func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {return CGFloat(rowHight)}func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {tableView.deselectRow(at: indexPath, animated: true)if (self.valueBlock != nil) {self.valueBlock!(indexPath.row)}}func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {return headerViewHight}func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {return headerViewHight + 5}func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {let view = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: headerViewHight))// 标题let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: headerViewHight))titleLabel.text = titleStringtitleLabel.font = UIFont.systemFont(ofSize: titleFont)titleLabel.textColor = UIColor.lightGraytitleLabel.textAlignment = .centerview.addSubview(titleLabel)// 线let lineView = UIView(frame: CGRect(x: 0, y: view.frame.size.height-1, width: kScreenWidth, height: 1))lineView.backgroundColor = UIColor(red: 220/250.0, green: 220/250.0, blue: 220/250.0, alpha: 1.0)view.addSubview(lineView)return view}func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {let footerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: footerViewHight))let lineView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: footerViewLineHight))lineView.backgroundColor = UIColor.lightGrayfooterView.addSubview(lineView)let btn = UIButton(type: .custom)btn.frame = CGRect(x: 0, y: footerViewLineHight, width: kScreenWidth, height: footerViewHight-footerViewLineHight)footerView.addSubview(btn)btn.setTitle("取消", for: .normal)btn.titleLabel?.font = UIFont.systemFont(ofSize: titleFont)btn.setTitleColor(UIColor.black, for: .normal)btn.addTarget(self, action: #selector(cancelBtnDidClick(btn:)), for: .touchUpInside)return footerView;}
}class TitleCell: UITableViewCell {let titleLabel = UILabel()override func awakeFromNib() {super.awakeFromNib()}override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {super.init(style: style, reuseIdentifier: reuseIdentifier)titleLabel.textAlignment = .centeraddSubview(titleLabel)}required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}override func layoutSubviews() {super.layoutSubviews()titleLabel.frame = self.bounds}
}

调用:

import UIKit
import SnapKit
class ViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.self.view.backgroundColor = .graylet btn = UIButton()btn.setTitle("Show Normal", for: UIControl.State())btn.setTitleColor(UIColor.black, for: UIControl.State())btn.layer.cornerRadius = 3self.view.addSubview(btn)btn.snp.makeConstraints { (make) -> Void inmake.left.equalTo(100)make.top.equalTo(self.view).offset(100)}let btn2 = UIButton()btn2.setTitle("Show List", for: UIControl.State())btn2.setTitleColor(UIColor.black, for: UIControl.State())btn2.layer.cornerRadius = 3self.view.addSubview(btn2)btn2.snp.makeConstraints { (make) -> Void inmake.left.equalTo(140)make.top.equalTo(self.view).offset(150)}btn.addTarget(self, action: #selector(popNormal), for: .touchUpInside)btn2.addTarget(self, action: #selector(popList), for: .touchUpInside)}@objc func popNormal()  {let acVC = BasePopView()present(acVC, animated: true, completion:  nil)}@objc func popList()  {let acVC = ListPopViewController(cellTitleList: ["List1", "List2", "List3", "List4"])!acVC.valueBlock = { index inprint(index)}acVC.cellTitleColor = UIColor.redacVC.cellTitleFont = 16acVC.titleString = "Lists"present(acVC, animated: true, completion:  nil)}}

希望大家高效、愉快撸码

Swift4.0 实现底部弹出框相关推荐

  1. flutter实现底部弹出框以及特色功能

    今天项目中要实现底部弹出框并且实现圆角功能,先来预览一下 可以看出实现的公告有 底部圆角,以及朋友圈,微信转发等 实现逻辑我直接代码列出 定义 List<String> nameItems ...

  2. html自定义js程序,JS中微信小程序自定义底部弹出框

    实现微信小程序底部弹出框效果,代码分为html,css和js两部分,具体代码详情大家参考下本文. html CSS .commodity_screen { width: 100%; height: 1 ...

  3. android的底部弹出框炫酷的样式,Android自定义底部弹出框ButtomDialog

    本文实例为大家分享了Android自定义底部弹出框的具体代码,供大家参考,具体内容如下 先看看效果和你要的是否一样 一 .先来配置自定义控件需要的资源 1.在res文件夹下创建一个anim文件夹并创建 ...

  4. 微信小程序商品详情页底部弹出框(点击加入购物车或立即购买弹出)

    项目实现效果如图 项目效果实现思路: wxml页面设计好底部栏<加入购物车,立即购买> 绑定点击触发弹出层函数 写好弹出窗效果 写好原始页面暗化效果 项目实现代码 1.wxml代码 (其中 ...

  5. android实现底部弹出框与软键盘冲突(全面屏虚拟键适配)

    普通的底部弹出框大家都可以很熟练的使用了,无非是一个diaolog的事情,但是当dialog中含有输入框之后,软键盘与dialog的冲突可以说是十分坑了...更别说加上底部虚拟键了,这么一个小功能磨得 ...

  6. 仿抖音评论底部弹出框(列表框+发表框)

    BottomSheetDialogFragment高仿抖音评论底部弹出框 先看效果图: 这个弹窗的效果是使用BottomSheetDialogFragment做的,第一个弹出的对话框为CommentL ...

  7. 【微信小程序封装底部弹出框二】

    [微信小程序封装底部弹出框二] <!--index.wxml--> <view><button style="margin-top: 300px;" ...

  8. 微信小程序 -- 自定义底部弹出框(带动画--滑入滑出)

    实现这么一个功能,点击选项进行选择,效果是从底部弹出选项框(带滑出动画),选择了某项或者点击其他地方,隐藏(带滑出动画).效果图如下: 可适用于任何场景,如普通选项(如图)或者类似商城小程序选择商品属 ...

  9. uni-app 封装底部弹出框

    一个很简单的代替 select 的组件: 这里只实现了弹出.确认时返回选中的选中的对象: 没有实现弹出时显示为第几个,有需要可以自己加: 位置.弹出动画.样式都可以根据自己的需要进行修改: 有问题或者 ...

最新文章

  1. 初识片选信号和中断控制器
  2. 从零开始学习jQuery (六) AJAX快餐
  3. 有什么值得推荐的Java Web练手项目?
  4. C++ public、protected、private区别
  5. 企业级应用架构(一) 三层架构之解耦
  6. Web安全之点击劫持(ClickJacking)
  7. 电脑排行榜笔记本_2019联想笔记本电脑排行榜
  8. Java基础,双色球系统实现,完整版本,没有BUG,完整代码版
  9. Django 中文文档解析
  10. 用python画猫咪怎么画-python画猫
  11. Python数据可视化:网易云音乐歌单
  12. 微信整人假红包图片_微信整人假红包10秒gif 微信整人红包动态图如何制作|动漫红包图...
  13. Vertica的这些事(四)—— 关于vertica常用函数介绍(持续更新ing)
  14. 移动的 ipcam 视频无处不在
  15. PAT(甲级)渡劫(一)-Public Bike Management
  16. 对antd中的表格筛选进行改造
  17. 无线无法解释服务器域名,科学网—Ubuntu 17.10 WIFI无线网络无法解析DNS域名的解决方法 - 徐勇刚的博文...
  18. python3根据excel表数据自动生成word格式数据报告
  19. 华硕ASUS VM591U内存条固态拆机安装教程
  20. h5(移动端) 监听软键盘弹起、收起

热门文章

  1. 58域内路由和域间路由
  2. NO 00004 iOS实现打砖块游戏 一 素材的制作
  3. SSL-ZYC 邮票
  4. 两种三角形的打印方法
  5. 解救小哈——广度优先搜索bfs
  6. Android多屏幕适配之字体大小、行间距和字间距
  7. 美团外卖饿了么竞品分析:共生存?还是你死我亡?
  8. python3 常用模块_python3-常用模块之re
  9. MCU--低功耗处理流程
  10. 微信小程序之在线任务发布与接单平台(2)