导航控制器上的后退按钮位置

时间:2016-07-25 18:39:41

标签: ios swift

我目前正在尝试开发iOS应用的自定义搜索。

我设法让搜索控制器出现并且搜索栏显示正常,但我唯一的问题是我需要后退按钮显示在导航栏的右侧而不是左侧,见下文

(正如你可以看到后面的按钮在左边,但我需要它在右边) http://imgur.com/qLPoIfG

这是我的代码:

import UIKit

class SearchTop10Controller:UITableViewController,UISearchResultsUpdating {

override func viewDidLoad() {
    super.viewDidLoad()

    let searchController = UISearchController(searchResultsController: self);

    self.definesPresentationContext = true;
    searchController.searchResultsUpdater = self;
    //        searchController.hidesNavigationBarDuringPresentation = true;

    searchController.dimsBackgroundDuringPresentation = false;
    searchController.searchBar.sizeToFit();
    self.navigationItem.titleView = searchController.searchBar;
    self.tableView.tableHeaderView = searchController.searchBar;
}

override func viewDidAppear(animated: Bool) {


}

func updateSearchResultsForSearchController(searchController: UISearchController) {
    //do whatever with searchController here.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

1 个答案:

答案 0 :(得分:3)

您可以向右侧栏项添加后退按钮,如下所示:

let backButton : UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "back_icon"), style: UIBarButtonItemStyle.Plain, target: self, action: #selector(back))
self.navigationItem.rightBarButtonItem = backButton

back_icon是您正在使用的图片,back是以下函数:

func back() {
    self.navigationController?.popViewControllerAnimated(true)
}

隐藏左栏项目:

self.navigationItem.leftBarButtonItem = nil

或:

self.navigationItem.hidesBackButton = true