登录后如何在 swift 4 中使用主视图控制器设置侧边菜单
Posted
技术标签:
【中文标题】登录后如何在 swift 4 中使用主视图控制器设置侧边菜单【英文标题】:After login how to set Sidemenu with MainView controller in swift 4 【发布时间】:2018-05-18 07:04:30 【问题描述】:请有人给我建议/指导。如何完成这个任务。我是从我的应用程序登录并在如何设置侧面菜单之后登录的。我已经添加了下面的代码。
这是 Appdeligate 代码
fileprivate func createMenuView()
// create viewController code...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewController(withIdentifier: "GSMngrMainViewController") as! GSMngrMainViewController
let leftViewController = storyboard.instantiateViewController(withIdentifier: "LeftViewController") as! LeftViewController
let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)
UINavigationBar.appearance().tintColor = UIColor.black
leftViewController.mainViewController = nvc
let slideMenuController = GSExSlideMenuController(mainViewController:nvc, leftMenuViewController: leftViewController)
slideMenuController.automaticallyAdjustsScrollViewInsets = true
slideMenuController.delegate = mainViewController
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = slideMenuController
//self.window?.rootViewController = loginViewController
self.window?.makeKeyAndVisible()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
// Override point for customization after application launch.
DropDown.startListeningToKeyboard()
let userId = UserDefaults.standard.string(forKey: "userId")
if userId == nil
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController = storyboard.instantiateViewController(withIdentifier: "GSLoginViewController")
let navigationCont = UINavigationController.init(rootViewController: loginViewController)
self.window?.rootViewController = navigationCont
else
self.createMenuView()
GMSServices.provideAPIKey(googleApiKey)
return true
【问题讨论】:
自定义设计的 SideDrawer github.com/RockinGarg/Slide-Menu-Drawer.git 【参考方案1】:将此代码添加到您希望拥有 SideMenu 的 ViewController 中
var customView = Side_Menu_ViewController()
func openSideMenu()
let controller = storyboard!.instantiateViewControllerWithIdentifier("Side_Menu_ViewController") as! Side_Menu_ViewController
addChildViewController(controller)
controller.view.frame = view.frame
controller.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
view.addSubview(controller.view)
controller.didMoveToParentViewController(self)
func hideMenu()
UIView.transitionWithView(self.view, duration: 0.5, options: UIViewAnimationOptions.OverrideInheritedOptions, animations:
self.customView.view.frame = CGRectMake(self.customView.view.frame.origin.x + 300, 0,self.customView.view.frame.size.width+120,self.view.frame.height);
, completion: (finished: Bool) -> () in
)
使用如下图所示的两个 UIView - 一个带有黑色 bg,另一个带有您需要的东西,并在 SideMenuViewController 的 viewDidLoad 中添加以下代码
SideView.frame = CGRectMake(self.view.frame.width, 0,SideView.frame.size.width,self.view.frame.height)
UIView.transitionWithView(self.view, duration: 0.5, options: UIViewAnimationOptions.OverrideInheritedOptions, animations:
self.SideView.frame = CGRectMake(self.SideView.frame.origin.x - 210, 0,self.SideView.frame.size.width-130,self.view.frame.size.height);
, completion: (finished: Bool) -> () in )
【讨论】:
感谢您的回答。但我必须在登录 API 获得成功状态后进行设置。如果 response.response?.statusCode == 200let storyboard = UIStoryboard(name: "Main", bundle: nil) let controller = storyboard.instantiateViewController(withIdentifier: "GSMngrMainViewController") as! GSMngrMainViewController self.navigationController?.pushViewController(controller, animated: true) 我知道这是错误的,但我想在这里设置边 munu 控制器 然后就可以调用里面的openSideMenu函数了 在你希望它出现的地方调用函数openSideMenu 你试过了吗? @Appu以上是关于登录后如何在 swift 4 中使用主视图控制器设置侧边菜单的主要内容,如果未能解决你的问题,请参考以下文章
swift 4 - segue 动画完成后如何在目标视图控制器中运行代码?
Swift 3.0:使用 URLSessionDownloadDelegate 完成下载后如何在不同的视图控制器中打开 PDF
如何在使用当前模式打开另一个视图控制器后设置导航项的标题 - Swift
swift 4 中使用 performSegue 打开的关闭页面时,如何在视图控制器和 TableViewController 之间传递数据?