問題描述
好的,我在 UIScrollView
中有幾個 UITextFields
和 UITextViews
,我想將鍵盤設置為隨時消失scrollview
被觸摸或滾動(當然,當您在文本字段/視圖內(nèi)觸摸時除外).
Alright, I have a couple of UITextFields
and UITextViews
inside a UIScrollView
, and I'd like to set the keyboard to disappear whenever the scrollview
is touched or scrolled (except when you touch down inside the text field/view, of course).
我目前的嘗試是將 UIScrollView
替換為子類,并將其設置為在 touchesBegan 內(nèi)調(diào)用 removeKeyboard 函數(shù)(在主視圖控制器內(nèi)定義)方法.但是,這只會在正常觸摸時移除鍵盤,而不是在簡單地滾動視圖時.那么,在 UIScrollView
中移除鍵盤的最佳方法是什么?
My current attempt at doing this is replacing the UIScrollView
with a subclass, and setting it to call a removeKeyboard function (defined inside the main view controller) inside the touchesBegan method. However, this only removes the keyboard for a normal touch, not when the view is simply scrolled. So, what's the best way to remove the keyboard inside a UIScrollView
?
提前感謝您的幫助.
推薦答案
這是在 iOS 7.0 及更高版本中實現(xiàn)此目標的最簡潔方法.
Here is the cleanest way to achieve this in iOS 7.0 and above.
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
或者
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
在斯威夫特中:
scrollView.keyboardDismissMode = .onDrag
或者
scrollView.keyboardDismissMode = .interactive
這篇關(guān)于關(guān)閉 UIScrollView 中的鍵盤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!