使用swift在选项卡式应用程序中以模态显示和关闭视图

时间:2018-09-25 14:49:14

标签: ios swift appdelegate modalviewcontroller presentmodalviewcontroller

我已经创建了一个选项卡式应用程序,并能够使用下面的代码以模态形式显示视图,但是,我一直坚持关闭视图并显示选项卡栏和第一个视图控制器。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let signUpViewController = storyboard.instantiateViewController(withIdentifier:"SignUpViewController") as! SignUpViewController
self.window?.makeKeyAndVisible()
self.window?.rootViewController = signUpViewController

3 个答案:

答案 0 :(得分:0)

signUpViewController内,在为tabBarController提供IB中的情节提要标识符后执行此操作

let tab = storyboard!.instantiateViewController(withIdentifier:"tab") 
UIApplication.shared.keyWindow?.rootViewController = tab

OR

(UIApplication.shared.delegate as! AppDelegate).window?.rootViewController = tab

答案 1 :(得分:0)

您应在标签栏vc上方显示模态vc

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let signUpViewController = storyboard.instantiateViewController(withIdentifier:"SignUpViewController") as! SignUpViewController
signUpViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
signUpViewController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.window.rootViewController.present(myModalViewController, animated: true, completion: nil)

(如果self.window.rootViewController-您的标签栏视图控制器)

在这种情况下,您可以在模态vc中使用 dismiss(动画:true,完成:nil) 方法将其关闭。

答案 2 :(得分:0)

在您的TabBarViewController中,在显示SignUpViewController时执行以下操作

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier:"SignUpViewController")
viewController.modalPresentationStyle = .overFullScreen
self.presentViewController(viewController, animated:true)

并且在关闭时,只需从SignUpViewController

调用关闭代码

例如dismissViewControllerAnimated