我有5个嵌入TabbarController的ViewController。我创建了一个Tabbar类来自定义我的Tabbar,例如:
class Tabbar: UITabBarController,UITabBarControllerDelegate {
var tabBarIteam = UITabBarItem()
@IBOutlet weak var tabbar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
// THIS IS FOR FİRST TABBAR ITEM
let selectedImage1 = UIImage(named: "vitrin_active")?.withRenderingMode(.alwaysOriginal)
let deSelectedImage1 = UIImage(named: "vitrin_deactive")
tabBarIteam = self.tabBar.items![0]
tabBarIteam.image = deSelectedImage1
tabBarIteam.selectedImage = selectedImage1
.... I HAVE ALSO 4 MORE.
}
在我的firstViewController中,有一个按钮动作
@IBAction func ChangeTabbarimageAndAction(_ sender: Any) {
..
}
我想在点击FirstView的Tabbar
时更改ChangeTabbarimageAndAction
的图像和动作(例如推入)。这可能吗?如果是,我该怎么办?我搜索了SO,但找不到任何解决方案。
答案 0 :(得分:2)
如果您想更改当前UIViewController
的标签栏图像或标题,则可以从tabBarItem
访问UIViewController
,并可以更改其属性,如下所示:
@IBAction func ChangeTabbarimageAndAction(_ sender: Any) {
tabBarItem.image = UIImage(named: "New Image name")
tabBarItem.selectedImage = UIImage(named: "NewSelectedImageName")
tabBarItem.title = "New Title"
}
答案 1 :(得分:1)
您可以在标签栏的委托方法中更改操作
class yourclass: UIViewController, UITabBarDelegate {
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("push or present action")
}
}
要设置图像,您可以使用
let firstViewController:UIViewController = UIViewController()
// The following statement is what you need
let customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME"), selectedImage: UIImage(named: "YOUR_IMAGE_NAME"))
firstViewController.tabBarItem = customTabBarItem