如何从标签栏 Controller 中以模态方式呈现 View ,以便 View 覆盖实际 View ?
我想用相机构建一个 View 。就像“WhatsApp”或“Instagram”一样,中间有一个按钮,用户可以单击该按钮并显示相机 View 。
此外,用户应该移动他在点击关闭按钮之前所在的选项卡。
最佳答案
我不得不在我目前正在构建的应用程序中实现类似的东西,它相对简单,您需要实现 UITabBarController
的委托(delegate)方法为了实现这一目标。
您需要实现的委托(delegate)方法是:
tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
从此方法返回 false 将阻止选项卡 Controller 选择您的选项卡,然后您只需要实现自己的逻辑来呈现 UIViewController
以编程方式。
这是一个例子:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// If your view controller is emedded in a UINavigationController you will need to check if it's a UINavigationController and check that the root view controller is your desired controller (or subclass the navigation controller)
if viewController is YourViewControllerClass {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "storyboardID") as? YourViewControllerClass {
controller.modalPresentationStyle = .fullScreen
self.present(controller, animated: true, completion: nil)
}
return false
}
// Tells the tab bar to select other view controller as normal
return true
}
我没有测试上面的代码,因为我的实现略有不同并且有更多变量。大体原理相同。
让我知道您的进展情况,如有必要,我会更新答案。
关于ios - 从选项卡栏 Controller 以模态方式呈现 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46071125/