我知道如何关闭键盘,我使用这个扩展:
extension UIViewController
{
func hideKeyboardWhenTappedAround()
{
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}
func dismissKeyboard()
{
view.endEditing(true)
}
}
并在viewDidLoad
中调用hideKeyboardWhenTappedAround
但我现在的问题是我向 navigationBar
添加了 UITextField
,并且此扩展不再起作用!
这就是我添加 UITextField
的方式:
let textField = UITextField(frame: CGRectMake(0,0,textfieldW,0.8*ram.navigationBarHeight) )
textField.borderStyle = UITextBorderStyle.RoundedRect
textField.center.y = centerView.center.y
centerView.addSubview(textField)
self.navigationItem.titleView = centerView
如何关闭从潜伏在导航栏中的 UITextField
带来的键盘?
最佳答案
引用此文本字段,例如:
var navigationBarField : UITextField?
然后初始化它:
navigationBarField = UITextField(frame: CGRectMake(0,0,textfieldW,0.8*ram.navigationBarHeight) )
textField.borderStyle = UITextBorderStyle.RoundedRect
textField.center.y = centerView.center.y
centerView.addSubview(navigationBarField)
self.navigationItem.titleView = centerView
当你想删除键盘调用时:
navigationBarField?.resignFirstResponder()
关于ios - 如何关闭导航栏中文本字段调用的键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38562968/