Swift4.0 实现底部弹出框
生活随笔
收集整理的這篇文章主要介紹了
Swift4.0 实现底部弹出框
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
網上找了些例子,但是并不能滿足我的要求,下面我將網上的荔枝與自己的改進分享給大家
原理: 通過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 实现底部弹出框的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何提高爬虫效率
- 下一篇: 东北大学软件项目管理与过程改进复习提纲(