在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
当前回答
用另一种方法解决了这个问题。
Create a button that will be placed in the background From the Attribute Inspector, change the button type to custom, and the makes the button transparent. Expand the button to cover the whole view, and make sure the button is behind all the other object. Easy way to do this is to drag the button to the top of list view in the View Control drag the button to the viewController.h file and create an action (Sent Event: Touch Up Inside) like : (IBAction)ExitKeyboard:(id)sender; In ViewController.m should look like : (IBAction)ExitKeyboard:(id)sender { [self.view endEditing:TRUE]; } Run app, and when you click away from the TextView, the keyboard disappears
其他回答
就像matt对samvermette的评论一样,我也不喜欢检测“\n”的想法。返回键在UITextView中是有原因的,当然是去下一行。
在我看来,最好的解决方案是模仿iPhone的消息应用程序-这是在键盘上添加工具栏(和按钮)。
我从以下博客文章中得到了代码:
http://www.iosdevnotes.com/2011/02/iphone-keyboard-toolbar/
步骤:
-添加工具栏到你的XIB文件-设置高度为460
-添加工具栏按钮项(如果尚未添加)。如果需要右对齐,还可以将灵活的工具栏按钮项添加到XIB,并移动工具栏按钮项
-创建将按钮项目链接到resignFirstResponder的动作,如下所示:
- (IBAction)hideKeyboard:(id)sender {
[yourUITextView resignFirstResponder];
}
套:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect frame = self.keyboardToolbar.frame;
frame.origin.y = self.view.frame.size.height - 260.0;
self.keyboardToolbar.frame = frame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect frame = self.keyboardToolbar.frame;
frame.origin.y = self.view.frame.size.height;
self.keyboardToolbar.frame = frame;
[UIView commitAnimations];
}
//你可以使用这个…
步骤1。第一步是确保声明了对UITextViewDelegate协议的支持。这是在你的头文件中完成的,例如这里的头文件叫做
EditorController。h:
@interface EditorController : UIViewController {
UITextView *messageTextView;
}
@property (nonatomic, retain) UITextView *messageTextView;
@end
步骤2。接下来你需要将控制器注册为UITextView的委托。从上面的例子继续,这里是我如何用EditorController作为委托初始化UITextView…
- (id) init {
if (self = [super init]) {
// define the area and location for the UITextView
CGRect tfFrame = CGRectMake(10, 10, 300, 100);
messageTextView = [[UITextView alloc] initWithFrame:tfFrame];
// make sure that it is editable
messageTextView.editable = YES;
// add the controller as the delegate
messageTextView.delegate = self;
}
步骤3。现在拼图的最后一块是采取行动响应shouldCahngeTextInRange消息如下:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:@"\n"]) {
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
}
对于Xcode 6.4。Swift 1.2。:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
super.touchesBegan(touches, withEvent: event)
if let touch = touches.first as? UITouch
{
self.meaningTextview.resignFirstResponder()
}
}
我的诀窍是:
1-创建一个按钮覆盖整个视图; 2-将它发送到视图的后台, 3-在属性检查器中将它的类型从“圆角矩形”改为“自定义” 4-创建一个动作 5-实施动作方法:
- (IBAction)bgTouched:(id)sender
{
//to dismiss keyboard on bg btn pressed
[_userInput resignFirstResponder];
}
哪里_userInput是你的TextField出口
UITextView没有任何在用户点击返回键时被调用的方法。如果您希望用户只能添加一行文本,请使用UITextField。按回车键并隐藏UITextView的键盘不符合接口准则。
即使这样,如果你想这样做,实现textView:shouldChangeTextInRange:replacementText:方法的UITextViewDelegate,并在检查替换文本是否\n,隐藏键盘。
也许还有别的办法,但我不知道有什么办法。