我正在使用一个包含 5 个选项卡的选项卡栏 Controller 。在 tab1 中,我有一个按钮可以将我带到 tab2。该 tab2 嵌入在导航 Controller 中。
那么当我通过segue从tab1来时,如何使Tab栏隐藏在tab2中?
在 Storyboard 中,我将Hide Bottom bar on Push
激活。另外,我在 View 中编写了 self.tabBarController?.tabBar.isHidden = true
加载了 tab2。在tab1中我的prepareForSegue是这样的
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShortcutSegue" {
let tabVc = segue.destination as! UITabBarController
tabVc.selectedIndex = 1
tabVc.tabBarController?.tabBar.isHidden = true
}
}
对于 tab2 View Controller ,您可以编写以下代码来隐藏选项卡栏。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let destinationTabBar = segue.destinationViewController as? UITabBarController {
if segue.identifier == "ShortcutSegue" {
destinationTabBar.viewControllers?.removeAtIndex(adminScreenIndex)
}
}
}
或者您可以在您的重写函数prepareForSegue 方法中编写以下代码。
if let tabVc = segue.destinationViewController as? tab2ViewController {
tabVc.hidesBottomBarWhenPushed = true
}
或者您可以在属性检查器的主 Storyboard 中为选项卡栏 View Controller 勾选“按下时隐藏按钮栏”,如下图所示。
Main storyboard attribute inspector "Hide Button Bar On Push"
Tôi là một lập trình viên xuất sắc, rất giỏi!