簡體   English   中英

Swift如何在Button按鈕上顯示Tabbar

[英]Swift How to present Tabbar on Button click

在我的項目中,我想在按鈕單擊時顯示Tabbar,現在我已經創建了tabbar,我將身份名稱作為“tabbar”,我在下面的圖像中顯示

在此輸入圖像描述

所以現在我使用下面的代碼來調用Tab欄控制器,但我沒有得到它。

 let tabbar: UITabBarController? = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)

 navigationController?.pushViewController(tabbar, animated: true)

你能告訴我我需要做什么,以及哪些代碼對我來說是有用的提示Tabbar控制器。

更多規格:我添加下面的圖像,其中一個viewController中有藍色按鈕我想在點擊該藍色按鈕時顯示標簽欄控制器

在此輸入圖像描述

謝謝。

試試看,看看:
使用Storyboard Segue:使用操作按鈕以模態方式連接您的segue。

在此輸入圖像描述

以編程方式 :使用視圖控制器(UIViewController)的self並以模態方式呈現它,就像這樣。

if let tabbar = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController) {
    self.present(tabbar, animated: true, completion: nil)
}

結果如下:

在此輸入圖像描述

使用這個,你沒有那里的導航控制器,這就是為什么它不會這樣推,相反,你需要使用以下代碼:

self.present(tabbar, animated: true, completion: nil)

您應該知道Apple的HIG(人機接口指南)說如果您有一個選項卡式應用程序,它應該是整個應用程序的根級導航。 從人機界面的角度來看,你不應該做你想做的事情。

也就是說,技術上應該是可行的。

我的猜測是你沒有導航控制器。

使用調試器檢查self.navigationController的值,或添加print語句:

 let tabbar: UITabBarController? = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
 print("navigationController = \(navigationController)")
 print("tabbar = \(tabbar)")

 navigationController?.pushViewController(tabbar, animated: true)

如果navigationControllertabbar顯示為nil,那就是你的問題。

選擇觸發按鈕單擊的viewController,然后選擇Editor form form xcode menu-> Embed In-> NavigationController。

然后在按鈕操作中寫這個

   let tabbar: UITabBarController? = (self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)

    self.navigationController?.pushViewController(tabbar!, animated: true)

根據你的圖像,我認為你的tabBarController沒有后退按鈕。 意味着用戶無法回頭。

如果是這種情況,你可以這樣做。

    let tabBar = self.storyboard?.instantiateViewController(withIdentifier: "tabBar")
   let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = tabBar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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