我有方案:UITabBarViewController
(有3个标签)
在所有选项卡中,我不想在顶部显示导航菜单。
从第一个选项卡开始,我想从按钮点击中推送另一个视图控制器,这将有"返回"按钮(和顶部工具栏,使用"取消")
我尝试了一些方法 - 在storyboard
中使用push segue - 没有后退按钮。
可能是因为我没有导航视图控制器,所以我的导航堆栈是空的。
编程:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "AddCoinTableViewController") as! AddCoinTableViewController
self.present(nextViewController, animated:true, completion:nil)
如果我在导航控制器中嵌入标签,那么我有顶部工具栏(我不想要)。
任何想法如何制作?
答案 0 :(得分:6)
您无法使用UINavigationController
即可实现导航功能。我的意思是你必须自己做所有动画类的东西,我认为这不是一个好主意。而不是那样,您可以使用navigationBar
,如果您不想在某个viewController上显示override func viewWillApear() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
}
override func viewWillDisappear(animated: Bool) {
self.navigationController?.isNavigationBarHidden = false
}
,那么对于那些视图控制器,请执行以下操作。
{{1}}
答案 1 :(得分:2)
您可以将导航控制器嵌入到您的第一个标签控制器(或任何您想要的),并将其隐藏在您不想在其viewDidLoad上的控制器上,如下所示:
self.navigationController?.isNavigationBarHidden = true
这样做,您将能够在您推动的控制器上看到后退按钮,并且没有隐藏导航栏。
确保使用导航控制器按下控制器,如下所示:
self.navigationController?.pushViewController(YOUR VIEW CONTROLLER, animated: true)
答案 2 :(得分:2)
下面的代码将允许您创建自己的导航处理类,并拥有UINavigationController
具有的“推送”“弹出”动画。您可以创建一个新项目,将下面的粘贴复制到{{1并亲自看看..
现在您可以提供任何ViewController.swift
导航控制器功能..
UIViewController
查看用于测试的控制器:
import UIKit
class NavigationHandler : NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning, UINavigationBarDelegate {
private var isPresenting: Bool = false
private weak var controller: UIViewController?
init(controller: UIViewController) {
super.init()
self.controller = controller
controller.transitioningDelegate = self
let navigationBar = UINavigationBar()
controller.view.addSubview(navigationBar)
NSLayoutConstraint.activate([
navigationBar.leftAnchor.constraint(equalTo: controller.view.leftAnchor),
navigationBar.rightAnchor.constraint(equalTo: controller.view.rightAnchor),
navigationBar.topAnchor.constraint(equalTo: controller.view.safeAreaLayoutGuide.topAnchor)
])
navigationBar.translatesAutoresizingMaskIntoConstraints = false
navigationBar.delegate = self
let item = UINavigationItem(title: controller.title ?? "")
let barButton = UIBarButtonItem(title: "Back", style: .done, target: self, action: #selector(onBackButton(button:)))
item.leftBarButtonItems = [barButton]
navigationBar.setItems([item], animated: true)
}
func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
@objc
private func onBackButton(button: UIBarButtonItem) {
self.controller?.dismiss(animated: true, completion: nil)
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.isPresenting = true;
return self;
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.isPresenting = false;
return self;
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.25;
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let duration = self.transitionDuration(using: transitionContext)
let fromController = transitionContext.viewController(forKey: .from)
let toController = transitionContext.viewController(forKey: .to)
let containerView = transitionContext.containerView
if self.isPresenting {
let frame = fromController!.view.frame
containerView.addSubview(toController!.view)
toController?.view.frame = CGRect(x: frame.origin.x + frame.width, y: frame.origin.y, width: frame.width, height: frame.height)
UIView.animate(withDuration: duration, animations: {
fromController?.view.frame = CGRect(x: frame.origin.x - frame.size.width, y: frame.origin.y, width: frame.size.width, height: frame.size.height)
toController?.view.frame = frame
}, completion: { (completed) in
transitionContext.completeTransition(true)
})
}
else {
let frame = fromController!.view.frame
containerView.insertSubview(toController!.view, at: 0)
toController?.view.frame = CGRect(x: frame.origin.x - frame.size.width, y: frame.origin.y, width: frame.size.width, height: frame.size.height)
UIView.animate(withDuration: duration, animations: {
fromController?.view.frame = CGRect(x: frame.origin.x + frame.width, y: frame.origin.y, width: frame.width, height: frame.height)
toController?.view.frame = frame
}, completion: { (completed) in
transitionContext.completeTransition(true)
})
}
}
}