问题描述
如何从UITabBar导航到另一个ViewController? 我正在使用UITabBarController。谁有4个TabBar Button,我在UITabBar的中间添加了一个大Button。因此,当我单击大的中间按钮时,我想导航到新的viewController。
实际上,我在此处添加了大按钮,以对UITabBar()进行子类化。我给你UITabBar()的所有代码 所以,请告诉我我该怎么做。现在一切正常,但是只有导航到新的视图控制器不起作用。
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表示在dismiss或PopViewController上。
或者是否还有其他解决方案可在UItabBarController的中间创建一个大按钮。单击按钮中的任意位置可跳至新的ViewController,然后单击关闭可返回到先前的控制器。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)