[英]Tab bar item as button
我的標簽欄中有 5 個標簽欄項目,其中 4 個有導航控制器的 segue,這些導航控制器通向視圖控制器。 我想讓中間的標簽欄項目充當一個按鈕,這樣當我點擊它時,我就可以控制發生的事情。
目前,我的中間標簽欄項目也連接到導航控制器,這是不對的,因為現在當我單擊標簽欄項目時,它會打開一個黑色的導航控制器。 如何將中間選項卡欄項轉換為按鈕,而不是轉到導航控制器?
如果我刪除導航控制器,它也會從選項卡欄中刪除選項卡欄項目。
如果你希望你的標簽欄項目作為一個按鈕,你可以繼承一個UITabBarController
並實現這個委托函數:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// Check if bar item selected is center
if viewController.tabBarItem.tag == 2 {
// Do Something Here ...
// Present View Controller
guard let navigationController = storyboard?.instantiateViewController(withIdentifier: "NavigationController") as? UINavigationController else { return false }
present(navigationController, animated: true)
// Returning false will not open the connected tab bar item view controller you attached to it
return false
}
// Return true to open the connected tab bar item view controller you attached to it (e.x. everything but the center item)
return true
}
實現自定義標簽欄並像普通按鈕一樣使用標簽欄項目
class ViewController: UIViewController, UITaBarDelegate {}
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { if item.tag == 1 { //Do something } if item.tag == 2 { //Do something } ....... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.