簡體   English   中英

標簽欄項目作為按鈕

[英]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
    }

實現自定義標簽欄並像普通按鈕一樣使用標簽欄項目

  1. 將新的 Tab Bar 視圖添加到您的視圖控制器 (VC)
  2. 添加您需要的自定義標簽欄項目並為其分配標簽編號(在屬性檢查器上)
  3. 將 Tab bar View 中的委托出口添加到您的視圖控制器(右鍵單擊並拖動到 VC)
  4. 在你的視圖控制器上,子類 UITabBarDelegate,像這樣
class ViewController: UIViewController, UITaBarDelegate {}
  1. 實現這個委托功能,那么它應該可以工作
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM