簡體   English   中英

將 UITextField 添加到導航項的標題視圖

[英]Add UITextField to title view of navigation item

如何將文本字段添加到導航項的標題視圖並使其大小正確?

如果我用這樣的搜索欄來做這件事,它工作正常,但我不知道如何讓文本字段以與搜索欄相同的方式調整自身大小。

myViewController.navigationItem.titleView = [UISearchBar new];

正如我的評論所建議的那樣,請嘗試以下操作,在我看來這就是您所追求的。

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

self.navigationItem.titleView = textField;

[textField becomeFirstResponder];
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..."

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM