我目前正在尝试为iOS应用开发自定义搜索。
尽管我唯一的问题是我需要使后退按钮显示在导航栏的右侧而不是左侧,但是我设法使搜索控制器和搜索栏正确显示,请参见下文
(如您所见,后退按钮在左侧,但我需要在右侧)
http://imgur.com/qLPoIfG
这是我的代码:
import UIKit
类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.
}
最佳答案
您可以向右栏项目添加后退按钮,如下所示:
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
关于ios - 导航 Controller 上的后退按钮位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38575189/