SideMenu 是一个易于使用的侧边菜单容器控制器,使用 Swift 5 编写。
除了侧边菜单应有的所有功能外,它还支持
菜单位置 / 状态菜单行为
上方 + 无 | 下方 + 滑动 |
---|---|
![]() |
![]() |
并排 + 淡入淡出 | 并排 + 菜单时隐藏 |
---|---|
![]() |
![]() |
我们称左/右视图控制器为菜单视图控制器,中心视图控制器为内容视图控制器。
对于 Swift 5.0 及更高版本,请使用 2.0.0 或更高版本。
要使用 CocoaPods 安装 SideMenu
,请在您的 Podfile
中添加以下行
pod 'SideMenuSwift'
# Note it's NOT 'SideMenu'
要使用 Carthage 安装 SideMenu
,请在您的 Cartfile
中添加以下行
github "kukushi/SideMenu" "master"
+
以添加新软件包。https://github.com/kukushi/SideMenu.git
粘贴到搜索字段中。import SideMenu
来导入 SideMenu
。SideMenu
SideMenuController
,Module 更改为 SideMenuSwift
。SideMenuController
为它们分别添加一个 Custom segue。SideMenu.Menu
,Class 更改为 SideMenuSegue
,Module 更改为 SideMenuSwift
。SideMenu.Content
,Class 更改为 SideMenuSegue
,Module 更改为 SideMenuSwift
。SideMenuController
的Attribute inspector。SideMenu
的应用程序import UIKit
import SideMenuSwift
// If you are using Carthage, uses `import SideMenu`
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
@objc func applicationDidFinishLaunching(_ application: UIApplication) {
let contentViewController = ...
let menuViewController = ...
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = SideMenuController(contentViewController: contentViewController,
menuViewController: menuViewController)
window?.makeKeyAndVisible()
return true
}
}
使用 UIViewController
扩展中提供的 sideMenuController
方法来获取父 SideMenuController
viewController.sideMenuController?.revealMenu()
SideMenu
的所有偏好设置都可以在 SideMenuController.preferences
中找到。建议查看 Example
以了解这些选项将如何生效。
SideMenuController.preferences.basic.menuWidth = 240
SideMenuController.preferences.basic.statusBarBehavior = .hideOnMenu
SideMenuController.preferences.basic.position = .below
SideMenuController.preferences.basic.direction = .left
SideMenuController.preferences.basic.enablePanGesture = true
SideMenuController.preferences.basic.supportedOrientations = .portrait
SideMenuController.preferences.basic.shouldRespectLanguageDirection = true
// See a lot more options on `Preferences.swift`.
SideMenu
最酷的功能之一是缓存。
// Cache the view controllers somewhere in your code
sideMenuController?.cache(viewController: secondViewController, with: "second")
sideMenuController?.cache(viewController: thirdViewController, with: "third")
// Switch to it when needed
sideMenuController?.setContentViewController(with: "second")
从 Storyboard 初始化的内容视图控制器呢?我们可以使用偏好设置来为其应用默认键!
SideMenuController.preferences.basic.defaultCacheKey = "default"
如果我们不想这么早加载所有内容视图控制器怎么办?我们可以使用延迟缓存
sideMenuController?.cache(viewControllerGenerator: { self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") }, with: "second")
sideMenuController?.cache(viewControllerGenerator: { self.storyboard?.instantiateViewController(withIdentifier: "ThirdViewController") }, with: "third")
SideMenu
在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。