可拖拽的模态框,弹出或菜单
一个使用UIScrollView的简单模态资源,允许用户通过向下拖动来关闭模态框。
将名为RCMantleViewController.swift
的文件拖到您的项目中。
设置非常简单,但我计划稍后使其更简单。
在Storyboard中添加新的ViewController,将Class
设置为RCMantleViewController
,将Storyboard ID
设置为MantleViewController
。
在Storyboard中添加您自己的视图,该视图代表模态框。还要为它设置一个唯一的Storyboard ID
。
如果您要使用弹出窗口,请设置背景视图颜色为透明。
将模块导入到您的父控制台(如果使用CocoaPods)
import MantleModal
在父控制台中按如下方式激活模态框。
// Create the MantleViewController from the Storyboard using the ID
let mantleViewController = storyboard!.instantiateViewControllerWithIdentifier("MantleViewController") as! RCMantleViewController
// Create your modal controller with your storyboard ID
let popUpViewController = storyboard!.instantiateViewControllerWithIdentifier("YourUniqueStoryboardID") as! YourViewController
// Set it's delegate to be able to call 'delegate.dismissView(animated: Bool)'
popUpViewController.delegate = mantleViewController
// Initialize Mantle
mantleViewController.setUpScrollView()
// Add your modal to Mantle
mantleViewController.addToScrollViewNewController(popUpViewController)
// Present the modal through the MantleViewController
self.presentViewController(mantleViewController, animated: false, completion: nil)
在模态框的控制器中,也导入以下内容
import MantleModal
然后定义您的代理助手。
var delegate: RCMantleViewDelegate!
然后使用此方法关闭模态框
delegate.dismissView(true)
MantleModal目前包含一些配置,您必须在使用mantleViewController.setUpScrollView
之前调用。当前可用的配置包括
// Allows you to dismiss the view by dragging up
mantleViewController.bottomDismissible = false
// Allows you to dismiss the view by dragging down
mantleViewController.topDismissable = true
// Allows you to drag to the sides to close
mantleViewController.draggableToSides = false
// 'appearOffset' moves the menu closer to the edge so that it appears quicker
mantleViewController.appearOffset = CGFloat(290)
// '290' could be the distance between the top of the popup and the top of the screen
// Makes the view present from the top, can be set multiple times before presenting
// Also makes it hide to the top.
mantleViewController.appearFromTop = true // // default = false
您还可以访问UIScrollView并修改其选项,只需确保在调用mantleViewController.setUpScrollView
后执行即可。
mantleViewController.scrollView.bounces = false
主要由我完成 - Ricardo Canales
最初的代码灵感来源于这个StackOverflow帖。向lbrendanl和Poql致谢