UITabBarController不使用UITabBarController 点击 使用自己的点击
如果你不想使用UITabBarController来处理点击事件,而是想要自定义点击事件,你可以使用UIButton来代替UITabBarItem,并在按钮的点击事件中处理你想要的逻辑。
首先,你需要创建自己的按钮,并将其添加到视图中。可以使用UIButton类来创建按钮,并设置按钮的frame和标题等属性。
let button1 = UIButton(frame: CGRect(x: 0, y: 0, width: view.bounds.width / 3, height: tabBarHeight))
button1.setTitle("Button 1", for: .normal)
button1.addTarget(self, action: #selector(button1Tapped), for: .touchUpInside)
view.addSubview(button1)
let button2 = UIButton(frame: CGRect(x: view.bounds.width / 3, y: 0, width: view.bounds.width / 3, height: tabBarHeight))
button2.setTitle("Button 2", for: .normal)
button2.addTarget(self, action: #selector(button2Tapped), for: .touchUpInside)
view.addSubview(button2)
let button3 = UIButton(frame: CGRect(x: (view.bounds.width / 3) * 2, y: 0, width: view.bounds.width / 3, height: tabBarHeight))
button3.setTitle("Button 3", for: .normal)
button3.addTarget(self, action: #selector(button3Tapped), for: .touchUpInside)
view.addSubview(button3)
然后,你可以在按钮的点击事件处理函数中实现你想要的逻辑。例如,可以在每个按钮的点击事件处理函数中切换到相应的视图控制器。
@objc func button1Tapped() {
// 切换到第一个视图控制器
self.selectedIndex = 0
}
@objc func button2Tapped() {
// 切换到第二个视图控制器
self.selectedIndex = 1
}
@objc func button3Tapped() {
// 切换到第三个视图控制器
self.selectedIndex = 2
}
这样就可以自定义处理点击事件,而不使用UITabBarController了
原文地址: https://www.cveoy.top/t/topic/ieEE 著作权归作者所有。请勿转载和采集!