如何在按下navigationBar时快速隐藏键盘

时间:2017-04-27 06:38:44

标签: ios swift swift3 uinavigationitem resignfirstresponder

我正在开发一个swift 3应用程序,每个页面都有一个导航栏。导航栏左侧还有一个按钮。 我需要在触摸屏幕上的任何对象时重新键盘。 我试着用通常的

self.navigationController?.navigationBar.endEditing(true)

但它适用于除导航栏以外的所有其他视图部分。 我尝试添加以下代码:

touchesBegan

<?xml version="1.0" encoding="UTF-8"?> <sequence name="main" xmlns="http://ws.apache.org/ns/synapse"> <in> <!-- Log all messages passing through --> <log level="full"/> <!-- ensure that the default configuration only sends if it is one of samples --> <!-- Otherwise Synapse would be an open proxy by default (BAD!) --> <filter regex="http://localhost:9000.*" source="get-property('To')" xmlns:ns="http://org.apache.synapse/xsd"> <then> <!-- Send the messages where they have been sent (i.e. implicit "To" EPR) --> <send/> </then> <else> <log/> <sequence key="gov:/HelloWorldIN"/> </else> </filter> </in> <out> <send/> </out> <description>The main sequence for the message mediation</description> </sequence> 功能,但它不起作用,因为我没有导航控制器,我只是在故事板中添加了搜索栏。

如何在导航栏上点击键盘,包括导航栏中的按钮?

这对我的应用非常重要,因为正如您在下面所看到的,它是用户可以在不更改视图的情况下点按的最大空间。

enter image description here

我需要在黄色区域内进行辞职工作,包括箭头所示的按钮。

4 个答案:

答案 0 :(得分:3)

例如

override func viewDidLoad() {
    super.viewDidLoad()
 let hideKeyboard = UITapGestureRecognizer(target: self, action: #selector(self.navigationBarTap))
    hideKeyboard.numberOfTapsRequired = 1
    navigationController?.navigationBar.addGestureRecognizer(hideKeyboard)

     }

并将操作处理为

 func navigationBarTap(_ recognizer: UIGestureRecognizer) {
    view.endEditing(true)
    // OR  USE  yourSearchBarName.endEditing(true)

}

答案 1 :(得分:1)

在您按下MENU按钮之前,在按钮操作中调用此行:

YourSearchOutlet.resignFirstResponder()

答案 2 :(得分:1)

尝试在导航栏上添加点按手势:

  override func viewDidLoad() {
        super.viewDidLoad()

        let navSingleTap = UITapGestureRecognizer.init(target: self, action: #selector(self.tap(_:)))
        navSingleTap.numberOfTapsRequired = 1
        self.navigationController?.navigationBar.subviews[1].isUserInteractionEnabled = true
        self.navigationController?.navigationBar.subviews[1].addGestureRecognizer(navSingleTap)
        }

    func tap() {
      //  do your work 
    }

答案 3 :(得分:1)

将UINavigationBar子类化,并将其设置为故事板中导航栏的自定义类。

$(document).ready