以编程方式使用带有 TabbarViewController 的 SWReavealController 创建幻灯片菜单 - swift
Create slide menu using SWReavealController with TabbarViewController programmatically - swift
我正在创建一个包含 TabBarViewController
的示例应用程序,我还使用 SWRevealViewController
实现了幻灯片菜单,但问题是它没有显示幻灯片菜单。
这里我设置rootViewController
在appdelegate
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = TabBarController()
这是我的 TabBarController
,我实现了 2 个 tabBar 项,第一个 tabBar 项应该有滑动菜单
let homeController = HomeController()
let homeNavigation = UINavigationController(rootViewController: homeController)
homeNavigation.tabBarItem.title = "Home"
let menuController = MenuViewController()
let swReveal = SWRevealViewController(rearViewController: homeNavigation, frontViewController: menuController)
swReveal?.toggleAnimationType = SWRevealToggleAnimationType.easeOut
swReveal?.toggleAnimationDuration = 0.30
let favController = FavoriteController()
let favNavigation = UINavigationController(rootViewController: favController)
favNavigation.tabBarItem.title = "Favorite"
viewControllers = [homeNavigation, favNavigation]
对于MenuController
,我设置了一些用于显示菜单的导航按钮
let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self.revealViewController(), action: #selector(slideMenu))
self.navigationItem.leftBarButtonItem = menuButton
@objc func slideMenu() {
if revealViewController() != nil {
revealViewController().revealToggle(animated: true)
revealViewController().rearViewRevealWidth = (view.bounds.width * 80 ) / 100
}
else {
print("no reveal view")
}
}
您需要在 didFinishLaunchingWithOptions
方法中配置 SWRevealViewController
,如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let frontNavigationController:UINavigationController
let rearNavigationController:UINavigationController
let revealController = SWRevealViewController()
var mainRevealController = SWRevealViewController()
frontNavigationController = UINavigationController(rootViewController: TabBarController())
rearNavigationController = UINavigationController(rootViewController: MenuViewController())
frontNavigationController.navigationBar.isHidden = true
rearNavigationController.navigationBar.isHidden = true
revealController.frontViewController = frontNavigationController
revealController.rearViewController = rearNavigationController
revealController.delegate = self
mainRevealController = revealController
window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = mainRevealController
self.window?.makeKeyAndVisible()
return true
}
并在您的 HomeController
中替换
let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self.revealViewController(), action: #selector(slideMenu))
和
let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self, action: #selector(slideMenu))
并删除
let menuController = MenuViewController()
let swReveal = SWRevealViewController(rearViewController: homeNavigation, frontViewController: menuController)
swReveal?.toggleAnimationType = SWRevealToggleAnimationType.easeOut
swReveal?.toggleAnimationDuration = 0.30
根据您的 TabBarController
,您的最终代码将是:
func customTabbar (){
let homeController = HomeController()
let homeNavigation = UINavigationController(rootViewController: homeController)
homeNavigation.tabBarItem.title = "Home"
let favController = FavoriteController()
let favNavigation = UINavigationController(rootViewController: favController)
favNavigation.tabBarItem.title = "Favorite"
let servicePhoneCollectionFLowLayoutInstance = UICollectionViewFlowLayout()
let serviceTabbarFlowLayoutInit = ExploreController(collectionViewLayout: servicePhoneCollectionFLowLayoutInstance)
let exploreController = serviceTabbarFlowLayoutInit
let exploreNavigation = UINavigationController(rootViewController: exploreController)
exploreNavigation.tabBarItem.title = "Explore"
let moreController = DetailViewController()
let moreViewController = UINavigationController(rootViewController: moreController)
moreViewController.tabBarItem.title = "Tools"
viewControllers = [homeNavigation, favNavigation, exploreNavigation, moreViewController]
}
有关更多信息,请查看您更新的项目 HERE。
这是SWRevealViewController
withUINavigationController
and[=44的工作代码=] UITabBarController
通过故事板 (Swift 4)
我正在创建一个包含 TabBarViewController
的示例应用程序,我还使用 SWRevealViewController
实现了幻灯片菜单,但问题是它没有显示幻灯片菜单。
这里我设置rootViewController
在appdelegate
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = TabBarController()
这是我的 TabBarController
,我实现了 2 个 tabBar 项,第一个 tabBar 项应该有滑动菜单
let homeController = HomeController()
let homeNavigation = UINavigationController(rootViewController: homeController)
homeNavigation.tabBarItem.title = "Home"
let menuController = MenuViewController()
let swReveal = SWRevealViewController(rearViewController: homeNavigation, frontViewController: menuController)
swReveal?.toggleAnimationType = SWRevealToggleAnimationType.easeOut
swReveal?.toggleAnimationDuration = 0.30
let favController = FavoriteController()
let favNavigation = UINavigationController(rootViewController: favController)
favNavigation.tabBarItem.title = "Favorite"
viewControllers = [homeNavigation, favNavigation]
对于MenuController
,我设置了一些用于显示菜单的导航按钮
let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self.revealViewController(), action: #selector(slideMenu))
self.navigationItem.leftBarButtonItem = menuButton
@objc func slideMenu() {
if revealViewController() != nil {
revealViewController().revealToggle(animated: true)
revealViewController().rearViewRevealWidth = (view.bounds.width * 80 ) / 100
}
else {
print("no reveal view")
}
}
您需要在 didFinishLaunchingWithOptions
方法中配置 SWRevealViewController
,如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let frontNavigationController:UINavigationController
let rearNavigationController:UINavigationController
let revealController = SWRevealViewController()
var mainRevealController = SWRevealViewController()
frontNavigationController = UINavigationController(rootViewController: TabBarController())
rearNavigationController = UINavigationController(rootViewController: MenuViewController())
frontNavigationController.navigationBar.isHidden = true
rearNavigationController.navigationBar.isHidden = true
revealController.frontViewController = frontNavigationController
revealController.rearViewController = rearNavigationController
revealController.delegate = self
mainRevealController = revealController
window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = mainRevealController
self.window?.makeKeyAndVisible()
return true
}
并在您的 HomeController
中替换
let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self.revealViewController(), action: #selector(slideMenu))
和
let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self, action: #selector(slideMenu))
并删除
let menuController = MenuViewController()
let swReveal = SWRevealViewController(rearViewController: homeNavigation, frontViewController: menuController)
swReveal?.toggleAnimationType = SWRevealToggleAnimationType.easeOut
swReveal?.toggleAnimationDuration = 0.30
根据您的 TabBarController
,您的最终代码将是:
func customTabbar (){
let homeController = HomeController()
let homeNavigation = UINavigationController(rootViewController: homeController)
homeNavigation.tabBarItem.title = "Home"
let favController = FavoriteController()
let favNavigation = UINavigationController(rootViewController: favController)
favNavigation.tabBarItem.title = "Favorite"
let servicePhoneCollectionFLowLayoutInstance = UICollectionViewFlowLayout()
let serviceTabbarFlowLayoutInit = ExploreController(collectionViewLayout: servicePhoneCollectionFLowLayoutInstance)
let exploreController = serviceTabbarFlowLayoutInit
let exploreNavigation = UINavigationController(rootViewController: exploreController)
exploreNavigation.tabBarItem.title = "Explore"
let moreController = DetailViewController()
let moreViewController = UINavigationController(rootViewController: moreController)
moreViewController.tabBarItem.title = "Tools"
viewControllers = [homeNavigation, favNavigation, exploreNavigation, moreViewController]
}
有关更多信息,请查看您更新的项目 HERE。
这是SWRevealViewController
withUINavigationController
and[=44的工作代码=] UITabBarController
通过故事板 (Swift 4)