通过SWReavealController和TabbarViewController以编程方式创建幻灯片菜单-Swift

时间:2018-07-11 06:15:26

标签: ios swift uitabbarcontroller tabbar swrevealviewcontroller

我正在创建一个包含<Image HeightRequest="150" BackgroundColor="LightGray" Source="{Binding NewImage}">的示例应用程序,还使用TabBarViewController实现幻灯片菜单,问题是幻灯片菜单没有显示出来。

我在SWRevealViewController中设置了rootViewController

appdelegate

这是我的 window = UIWindow(frame: UIScreen.main.bounds) window?.makeKeyAndVisible() window?.rootViewController = TabBarController() ,它实现了2个tabBar项,其中第一个tabBar项应具有幻灯片菜单

TabBarController

对于 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

1 个答案:

答案 0 :(得分:2)

您需要在SWRevealViewController方法中配置didFinishLaunchingWithOptions,如下所示:

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 UINavigationController UITabBarController 的工作代码情节提要(Swift 4)

https://stackoverflow.com/a/51725803/10150796