Add UITextField to title view of navigation item

前端 未结 2 1519
一向
一向 2021-02-20 09:27

How do you add a text field to the title view of a navigation item and have it sized correctly?

It works fine if I do this with a search bar like this but I don\'t know

相关标签:
2条回答
  • 2021-02-20 09:53

    As my comment suggested, try the following, which seems to me is what you're after.

    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, 21.0)];
    
    self.navigationItem.titleView = textField;
    
    [textField becomeFirstResponder];
    
    0 讨论(0)
  • 2021-02-20 09:56
    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..."
    
    0 讨论(0)
提交回复
热议问题