将UITextField添加到导航项的标题视图

时间:2014-07-26 22:30:37

标签: ios objective-c ios7 uitextfield

如何将文本字段添加到导航项的标题视图并使其大小正确?

如果我使用这样的搜索栏执行此操作,它可以正常工作但我不知道如何使文本字段以与搜索栏相同的方式调整大小。

myViewController.navigationItem.titleView = [UISearchBar new];

2 个答案:

答案 0 :(得分:28)

正如我的评论建议,请尝试以下操作,在我看来,这就是您所追求的目标。

UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, 21.0)];

self.navigationItem.titleView = textField;

[textField becomeFirstResponder];

答案 1 :(得分:0)

Class: ViewController: UISearchBarDelegate {

let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.searchController?.searchBar.delegate = self
navigationItem.searchController?.obscuresBackgroundDuringPresentation = false
navigationItem.searchController?.hidesNavigationBarDuringPresentation = false
navigationItem.searchController?.searchBar.placeholder = "Enter text here..."