Add UITextField to title view of navigation item

前端 未结 2 1467
一向
一向 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];
    

提交回复
热议问题