测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布日期最新发布 | 2016年9月 |
SPM支持 SPM | ✗ |
由 Mark Jackson,Mark Jackson 维护。
制作自定义模态框的一种简单而优秀的方法。
*
*
*
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
。
JModalController 要求 iOS 8 或更高版本。
JModalController 通过 CocoaPods 提供。为了安装它,请简单地将其添加到 Podfile 中。
pod 'JModalController'
创建一个与任何其他故事板控制器一样的故事板控制器。
根据需要调整控制器的大小。宽度和高度都可以短于呈现的视图控制器。
注意:请确保取消选中“从 NIB 调整视图大小”,否则模态框将不是您的自定义大小。如果不取消选中此选项,则必须在呈现模态框之前手动设置框架。
使用 Storyboard,您只需加载控制器并调用 presentModal()
#import JModalController
// ...
func showModal() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let simpleVC = storyboard.instantiateViewControllerWithIdentifier("SimpleModalViewController") as? SimpleModalViewController
//Set the delegate in order to dismiss the modal
simpleVC?.delegate = self
//Set configuration settings to customize how the modal presents
let config = JModalConfig(animationDuration: 0.2, tapOverlayDismiss: true, transitionDirection: .Bottom, backgroundTransform: true)
//Present the modal!
//`self` if no navigation or tabBar controllers are present!
presentModal(self, modalViewController: simpleVC!, config: config) {
print("Presented Simple Modal")
}
}
注意:
presentModal
需要一个basePresentingViewController
。此控制器应该是您要进行转换的基本控制器。因此,如果有您的控制器嵌入到 UINavigationController 和/或 UITabBarController 中,请使用navigationController
/tabBarController
而不是self
处理模态框很简单,只需调用 delegate.dismissModal(self, nil)
import JModalController
var delegate : JModalDelegate?
// ...
@IBAction func dismiss(sender: AnyObject) {
//Dismiss Modal, give the data parameter anything you want to send back to the presenting view controller.
self.delegate!.dismissModal(self, data: nil)
}
JModalConfig 是一个允许自定义模态框的类。属性描述动画长度、背景动画颜色等。
以下是可自定义的属性。可以通过初始化器或手动设置属性。有关这些属性的功能,请查看演示项目。
/// Background color of the overlay that covers your current view controller. Default set to UIColor(white: 0, alpha: 0.3)
public var overlayBackgroundColor: UIColor
/// Direction where you want the modal to appear from. Default set to .Bottom
public var transitionDirection: JModalTransitionDirection
/// View animation options for how to have modal and overlay to appear. Default set to .CurveLinear
public var animationOptions: UIViewAnimationOptions
/// Duration on how long views animate on appearing and dismissing. Default set to 0.3
public var animationDuration: NSTimeInterval
/// The 0 - 1 value of how much the background viewcontroller transforms on modal appearance. Default set to 0.93
public var backgroundTransformPercentage: Double
/// Boolean indicating of background viewcontroller should transform on modal appearance. Default set to true
public var backgroundTransform: Bool
/// Boolean indicating if tapping on the background overlay should dismiss modal. Default set to true
public var tapOverlayDismiss: Bool
///Swipe gesture directions you want your modal to support for dismissing. Default set to []
public var swipeDirections: [UISwipeGestureRecognizerDirection]
自动导入 pod 会使 UIViewController
遵守 JModalDelegate 协议。重写 dismissModal
方法可以获取模态视图返回的数据。
任何人都可以通过查找问题或提交任何 pull request 来帮助改进 JModalController。
Mark Jackson, [email protected]
JModalController 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。