Swift后退按钮到工具栏?

时间:2016-11-01 01:24:21

标签: ios swift user-interface

在我的项目中,我想在显示屏底部的工具栏中添加后退按钮。

我的UIViewController嵌入在导航控制器中,我也有底部工具栏。但默认情况下,后退按钮位于导航控制器的左上角。

3 个答案:

答案 0 :(得分:1)

您无法将按钮从navBat移回工具栏,但您可以添加自定义按钮。

self.tabBarController?.navigationItem.hidesBackButton = true
self.navigationController?.toolbarHidden = false
var items = [UIBarButtonItem]()
items.append(
    UIBarButtonItem( title: "Back", style: .Plain, target: self, action: #selector(gotoBack))
)
self.navigationController?.toolbar.items = items

并添加到ViewController函数

func gotoBack(){
    self.navigationController?.popViewController(animated: true)
}

答案 1 :(得分:0)

自定义navigationController

Swift 3.0

NavViewController.swift

class NavViewController: UINavigationController,UINavigationControllerDelegate {

    var _delegate:UIGestureRecognizerDelegate?


    override func viewDidLoad() {
        super.viewDidLoad()

        _delegate = self.interactivePopGestureRecognizer?.delegate
        self.delegate = self

    }



       func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
//Because it can slide
            if viewController != navigationController.viewControllers[0] {
                viewController.navigationController?.interactivePopGestureRecognizer?.delegate = nil;  
            }else{
                viewController.navigationController?.interactivePopGestureRecognizer?.delegate = _delegate;
            }
        }
    }

在NextViewController.swift中

override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        // Do any additional setup after loading the view.
    }
    @IBAction func BackButtonItem(_ sender: UIBarButtonItem) {
        _ = navigationController?.popViewController(animated: true)
    }

答案 2 :(得分:0)

 let toolbar = UIToolbar (frame: CGRect(x: 0, y: 0, width: 
self.view.frame.size.width, height: 56))

    toolbar.barStyle = UIBarStyle.black
    toolbar.sizeToFit()

    var items = [UIBarButtonItem]()

    items.append(
        UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(Viewcontroller.BackClicked(sender:)))
    )
    toolbar.setItems(items, animated: true)
    toolbar.tintColor = UIColor.black
self.navigationController?.navigationItem.hidesBackButton = true

self.navigationController?.toolbar.items = items

func BackClicked(sender: UIBarButtonItem)

 {
  _ = navigationController?.popViewController(animated: true)   

 }