如何在 UITabBarController 的特定索引中添加 TabBarItem?
我可以添加一個新的標簽欄專案,
[self.tabBarItems addObject:nav];
它總是添加在最后。我需要在特定的索引位置添加標簽欄專案。我該怎么做?
uj5u.com熱心網友回復:
這是一種插入新視圖選項卡欄項的方法:
import UIKit
class ViewController: UIViewController {
// button and textfields are defined in the storyboard
@IBOutlet var button: UIButton!
var tabbar: UITabBarController?
@IBOutlet var label: UITextField!
@IBOutlet var image: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
label.text = "Four"
image.text = "info.circle.fill"
tabbar = self.tabBarController
}
@IBAction func addVC(button: UIButton) {
// remove keyboard
label.resignFirstResponder()
image.resignFirstResponder()
// check that everything is set
if var tbVC = tabbar?.viewControllers,
let title = label.text,
let imageName = image.text {
// create new view controller
let newVC = UIViewController()
// vreate the tab bar item for the new view controller
let tabBarItem = UITabBarItem(title: title,
image: UIImage(systemName: imageName),
tag: tbVC.count 1)
newVC.tabBarItem = tabBarItem
// insert new tab in tab bar view controller
tbVC.insert(newVC, at: 2)
// update the teb bar controller
tabbar?.setViewControllers(tbVC, animated: true)
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/401933.html
上一篇:如何使用Scala在DynamoDB中查詢最接近時間戳的專案
下一篇:HStack中如何計算間距
