ios 使用滑动手势关闭键盘(如消息应用程序中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7133463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dismiss keyboard with swipe gesture (as in Message app)
提问by Michael Stet
When the keyboard is showing on the iPhone's Messages app, if the user begins a swipe down from the messages tableview and continues into the keyboard area, the keyboard will begin to dismiss. If they move their finger up and down during this process, the keyboard moves with it.
当键盘显示在 iPhone 的消息应用程序上时,如果用户开始从消息表视图向下滑动并继续进入键盘区域,键盘将开始关闭。如果他们在此过程中上下移动手指,键盘也会随之移动。
Are Apple doing this with private APIs, or is there a way to control the keyboard like this from (I presume) a gesture recognizer?
Apple 是使用私有 API 来做这件事,还是有办法从(我认为)手势识别器中像这样控制键盘?
回答by Daniel Amitay
I created a UIView category that provides the desired functionality:
我创建了一个提供所需功能的 UIView 类别:
https://github.com/danielamitay/DAKeyboardControl
https://github.com/danielamitay/DAKeyboardControl
Edit: It has indeed been used on the app store.
编辑:它确实已在应用商店中使用。
回答by ma11hew28
The simplest solution is to set the following two properties:
最简单的解决方案是设置以下两个属性:
Boom, baby!
砰,宝贝!
Check out Acani ChatsiPhone ClientChatViewController.swift
for an example.
回答by Miguel Carvajal
Luckily, Apple added the handy property keyboardDismissMode
on UIScrollView
to make your life a little easier.
幸运的是,苹果增加了方便的特性keyboardDismissMode
上UIScrollView
,使你的生活变得更轻松。
Now your app can behave like Messages.app just by changing a single property on your Storyboard, or alternatively by adding one line of code!
现在,您的应用程序可以像 Messages.app 一样运行,只需更改 Storyboard 上的单个属性,或者添加一行代码!
This property uses the new UIScrollViewKeyboardDismissMode
enum. The possible values of this enum are as follows:
此属性使用新的UIScrollViewKeyboardDismissMode
枚举。此枚举的可能值如下:
UIScrollViewKeyboardDismissModeNone // the keyboard is not dismissed automatically when scrolling
UIScrollViewKeyboardDismissModeOnDrag // dismisses the keyboard when a drag begins
UIScrollViewKeyboardDismissModeInteractive // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss
Here's the Storyboard property to change to dismiss the keyboard on scroll:
这是要更改以在滚动时关闭键盘的 Storyboard 属性:
Hope that helps solving your problem
希望能帮助解决您的问题
回答by Jeremy Massel
In iOS 7, you can now dismiss the keyboard interactively on a UIScrollView.
在 iOS 7 中,您现在可以在 UIScrollView 上以交互方式关闭键盘。
Dismissing the keyboard in a UIScrollView
Hope that helps someone.
希望能帮助某人。
回答by JonasG
You can use UISwipeGestureRecognizer to do so. I don't know how to implement it using code, but if you are using the new xcode 4.2 beta then heres an easy method:
您可以使用 UISwipeGestureRecognizer 来做到这一点。我不知道如何使用代码来实现它,但是如果您使用的是新的 xcode 4.2 beta,那么这里有一个简单的方法:
- Create an IBAction:
- 创建一个 IBAction:
- (IBAction)dismiss:(id)sender;
- (IBAction)dismiss:(id)sender;
Go to your view in your xib and set the class for you view to UIControl.
Drag and connect the UISwipeGestureRecognizer from the Library to your view.
Connect the IBAction (TouchDown) with the UISwipeGestureRecognizer.
Write the code to dismiss the keyboard:
- (IBAction)dismiss:(id)sender { [yourTextField resignFirstResponder]; }
转到您的 xib 中的视图并将视图的类设置为 UIControl。
将 UISwipeGestureRecognizer 从库中拖动并连接到您的视图。
将 IBAction (TouchDown) 与 UISwipeGestureRecognizer 连接起来。
编写代码以关闭键盘:
- (IBAction)dismiss:(id)sender { [yourTextField resignFirstResponder]; }
Done!
完毕!
回答by mangonob
In swiftyou can use below code to get the view container of current keyboard (if exist), than you can change is's frame in your code:
在swift 中,您可以使用下面的代码来获取当前键盘的视图容器(如果存在),然后您可以在代码中更改 is 的框架:
UIApplication.shared.windows
.filter{ NSStringFromClass(##代码##.classForCoder) == "UIRemoteKeyboardWindow" }
.first?.subviews.filter { NSStringFromClass(##代码##.classForCoder) == "UIInputSetContainerView" }
.first?.subviews.filter { NSStringFromClass(##代码##.classForCoder) == "UIInputSetHostView" }
.first
By the way here are a tool called Revealmake you can see the view hierarchical.
顺便说一下,这里有一个名为Reveal的工具,可以让您看到分层视图。
回答by Bassem
I think the best way is to place a hidden button above the text input container. a long strip and when it detects a touchdown and release or cancel then hide the keyboard.
我认为最好的方法是在文本输入容器上方放置一个隐藏按钮。一个长条,当它检测到触地得分并释放或取消时,隐藏键盘。
I'm going to try it and i will let you know how i go.
我要试试看,我会让你知道我是怎么做的。
回答by Nicolas Miari
Short answer; They're most likely doing some 'Private API' thing there.
简短的回答;他们很可能在那里做一些“私有 API”的事情。
I'm most sure that the keyboard is in an independent view, above your app's window (You do not have access/control to it and it always displays on top, no matter what). The most you can do is have the input view become/resign first responder status, and the keyboard will appear/disappear accordingly: All or nothing.
我很确定键盘位于应用程序窗口上方的独立视图中(您无权访问/控制它,无论如何它始终显示在顶部)。您最多可以做的是让输入视图成为/退出第一响应者状态,键盘将相应地出现/消失:全有或全无。
You may be able to get a handle of the keyboard view and change its frame property (using undocumented properties of documented classes, and undocumented classes), but I'm pretty sure that will get you kicked out of the store.
您可能能够获得键盘视图的句柄并更改其框架属性(使用已记录类的未记录属性和未记录类),但我很确定这会让您被踢出商店。