ios - 如何从 UITabBar 导航到另一个视图控制器?
问题描述
如何从 UITabBar 导航到另一个视图控制器?我正在研究 UITabBarController。谁有 4 个 TabBar 按钮,我在 UITabBar 中间添加了一个大按钮。所以我想在单击大中间按钮时导航到新的 viewController。
实际上我在这里添加了大按钮子类化 UITabBar()。我给你 UITabBar() 的所有代码所以请告诉我我该怎么做。现在一切正常,但只有 Navigateto 新的视图控制器不工作。
import UIKit
class MainTabBar: UITabBar {
private var middleButton = UIButton()
override func awakeFromNib() {
super.awakeFromNib()
setupMiddleButton()
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if self.isHidden {
return super.hitTest(point, with: event)
}
let from = point
let to = middleButton.center
return sqrt((from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y)) <= 39 ? middleButton : super.hitTest(point, with: event)
}
override func layoutSubviews() {
super.layoutSubviews()
middleButton.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 0)
}
func setupMiddleButton() {
middleButton.frame.size = CGSize(width: 70, height: 70)
middleButton.backgroundColor = .blue
middleButton.layer.cornerRadius = 35
middleButton.layer.masksToBounds = true
middleButton.setTitle("+", for: .normal)
middleButton.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 0)
middleButton.addTarget(self, action: #selector(largeButtonAction), for: .touchUpInside)
addSubview(middleButton)
}
@objc func largeButtonAction(_ sender:UIButton) {
print("Write your Navigation Code here.......")
jumpToHomeVC()
}
func jumpToHomeVC(){
let story = UIStoryboard(name: "Main", bundle:nil)
let vc = story.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
let nav = UINavigationController(rootViewController: vc)
nav.navigationController?.pushViewController(vc, animated: true)
}
}
我将我的 UITabBar() 子类化为我的自定义 MainTabBar()。
我想当我点击大按钮时转到新的 UIViewController 和当再次点击返回时来到 UITabBarController 意味着关闭或 PopViewController。
或者如果有任何其他解决方案可以在 UItabBarController 的中间创建一个大按钮。单击按钮中的任意位置以跳转到新的 ViewController 并在关闭时返回上一个控制器。
解决方案
推荐阅读
- java - 将 JSON 字符串反序列化为 Java POJO 列表列表
- python - 如何将多个参数从 pandas 数据帧传递给函数并将结果返回到数据帧中特定位置的数据帧
- sql - PSQL左外连接不返回不匹配连接的值
- java - 为我的课程中使用的通用 pojo 建模
- mpi - mpi4py 创建多个组并从每个组分散
- java - 尝试在空对象引用上调用虚拟方法“java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String)”
- angularjs - 如何在弹出窗口中显示 Steam OpenID 页面?
- forms - 如何使用 react 和 redux 更改表单中的输入值?
- java - 将默认 Toast 样式应用于自定义 Toast
- java - Java在第6列丢硬币时连接四个对角线获胜错误和错误