在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
当前回答
好的。每个人都用技巧给出了答案,但我认为实现这一点的正确方法是
将以下操作连接到接口生成器中的“Did End On Exit”事件。 (右键单击TextField并从“Did end on exit”中cntrl-drag到以下方法。
-(IBAction)hideTheKeyboard:(id)sender
{
[self.view endEditing:TRUE];
}
其他回答
我知道这个问题已经被回答过很多次了,但下面是我对这个问题的看法。
我发现samvermette和ribeto的回答非常有用,还有maxpower在ribeto的回答中的评论。但这些方法存在一个问题。matt在samvermette的回答中提到的问题是,如果用户想要在其中粘贴带有换行符的东西,键盘将隐藏而不粘贴任何东西。
所以我的方法是上述三种解决方案的混合,只有检查输入的字符串是否为新行,当字符串的长度为1时,我们确保用户是键入而不是粘贴。
以下是我所做的:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSRange resultRange = [text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet] options:NSBackwardsSearch];
if ([text length] == 1 && resultRange.location != NSNotFound) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
用另一种方法解决了这个问题。
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
对于Swift 3,这段代码允许我在UITextView之外按下键来解除键盘。
@IBOutlet weak var comment: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
comment.delegate = self
let tapGestureRecogniser = UITapGestureRecognizer(target: self, action: #selector(tap))
view.addGestureRecognizer(tapGestureRecogniser)
}
func tap(sender: UITapGestureRecognizer) {
if comment.isFirstResponder {
comment.resignFirstResponder()
}
}
对于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()
}
}
+ (void)addDoneButtonToControl:(id)txtFieldOrTextView
{
if([txtFieldOrTextView isKindOfClass:[UITextField class]])
{
txtFieldOrTextView = (UITextField *)txtFieldOrTextView;
}
else if([txtFieldOrTextView isKindOfClass:[UITextView class]])
{
txtFieldOrTextView = (UITextView *)txtFieldOrTextView;
}
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,
0,
[Global returnDeviceWidth],
50)];
numberToolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_return"]
style:UIBarButtonItemStyleBordered
target:txtFieldOrTextView
action:@selector(resignFirstResponder)];
numberToolbar.items = [NSArray arrayWithObjects:btnDone,nil];
[numberToolbar sizeToFit];
if([txtFieldOrTextView isKindOfClass:[UITextField class]])
{
((UITextField *)txtFieldOrTextView).inputAccessoryView = numberToolbar;
}
else if([txtFieldOrTextView isKindOfClass:[UITextView class]])
{
((UITextView *)txtFieldOrTextView).inputAccessoryView = numberToolbar;
}
}