gpt4 book ai didi

ios - 以编程方式在 subview 中切换标签栏 View (自定义标签栏 Controller )

转载 作者:行者123 更新时间:2023-11-28 08:23:57 24 4
gpt4 key购买 nike

因为想重新设计tab bar UI,所以根据https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Tab-Bar写了一个自定义的tab bar controller

TabBarViewController的viewDidLoad()中,定义每个标签栏对应的几个 subview

homeViewController = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")
...
viewControllers = [homeViewController, searchViewController, accountViewController, trendingViewController]

以及点击tab时的main方法

@IBAction func didPressTab(_ sender: UIButton) {

let previousIndex = selectedIndex
selectedIndex = sender.tag

tabButtons[previousIndex!].isSelected = false
let previousVC = viewControllers[previousIndex!]

// remove previous VC
previousVC.willMove(toParentViewController: nil)
previousVC.view.removeFromSuperview()
previousVC.removeFromParentViewController()

// set current VC
sender.isSelected = true
let vc = viewControllers[selectedIndex]

addChildViewController(vc)

// Adjust the size to match content view
vc.view.frame = contentView.bounds
contentView.addSubview(vc.view)
vc.didMove(toParentViewController: self)
}

我可以在加载标签栏 View 时设置默认标签栏索引 selectedIndex。但是,如何切换到 homeViewController 中的下一个选项卡栏(无需点击选项卡栏按钮)?

这在 homeViewController 中不起作用
TabBarViewController().tabButtons[2].isSelected = true TabBarViewController().didPressTab(TabBarViewController().tabButtons[2])

我不确定如何获取正在运行的选项卡 Controller 、设置 selectedIndex 以及在 subview Controller 中更新 subview 。

最佳答案

您需要做的就是调用 tabBar.setSelectedViewController: 并传递 View Controller 。

如果您只知道选项卡索引,则调用 tabBar.viewControllers[index] 并获取 View Controller 。

关于ios - 以编程方式在 subview 中切换标签栏 View (自定义标签栏 Controller ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40570755/

24 4 0