`

UITextView关闭键盘

    博客分类:
  • IOS
 
阅读更多
1.如果你程序是有导航条的,可以在导航条上面加多一个Done的按钮,用来退出键盘,当然要先实UITextViewDelegate。
- (void)textViewDidBeginEditing:(UITextView *)textView {  
   UIBarButtonItem *done =    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease];  
   self.navigationItem.rightBarButtonItem = done;      
}  
  
- (void)textViewDidEndEditing:(UITextView *)textView {  
    self.navigationItem.rightBarButtonItem = nil;  
}  
  
- (void)leaveEditMode {  
    [self.textView resignFirstResponder];  
}  


2.如果你的textview里不用回车键,可以把回车键当做退出键盘的响应键。
代码如下:
#pragma mark - UITextView Delegate Methods   
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text  
{  
    if ([text isEqualToString:@"\n"]) {  
        [textView resignFirstResponder];  
        return NO;  
    }  
    return YES;  
}  

这样无论你是使用电脑键盘上的回车键还是使用弹出键盘里的return键都可以达到退出键盘的效果。

3.第三种方法感觉效果比上面两种都好,就是在弹出的键盘上面加一个view来放置退出键盘的Done按钮。
- (void) initFeedText{
    //初始化 UITextView
    CGRect frame = CGRectMake(12, 0, 160, 88);
    textFeed = [[UITextView alloc] initWithFrame:frame];
    textFeed.delegate = self;
    textFeed.font = [UIFont fontWithName:@"TrebuchetMS" size:16];
    textFeed.backgroundColor = [UIColor clearColor];
    textFeed.autocorrectionType = UITextAutocorrectionTypeYes;
    textFeed.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textFeed.keyboardType = UIKeyboardTypeDefault;
    textFeed.returnKeyType = UIReturnKeyDone;
    textFeed.delegate = self;
    
    //在弹出的键盘上面加一个view来放置退出键盘的Done按钮
    UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];  
    [topView setBarStyle:UIBarStyleDefault];
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];    
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace, doneButton, nil]; 
    [btnSpace release];
    [doneButton release];
    [topView setItems:buttonsArray];
    [textFeed setInputAccessoryView:topView];
    [topView release];
}

//关闭键盘
-(void) dismissKeyBoard{
    [textFeed resignFirstResponder];
}


推荐第三种,很好用~
分享到:
评论

相关推荐

    IOS 开发UITextView回收或关闭键盘

     iOS开发中,发现UITextView没有像UITextField中textFieldShouldReturn:这样的方法,那么要实现UITextView关闭键盘,就必须使用其他的方法,下面是可以使用的几种方法。 1.如果你程序是有导航条的,可以在导航条...

    UiOS开发中ITextView回收或关闭键盘使用方法总结

    iOS开发中,发现UITextView没有像UITextField中textFieldShouldReturn:这样的方法,那么要实现UITextView关闭键盘,必须使用其他的方法,下面是可以使用的几种方法,需要的朋友参考下吧

    iOS关闭虚拟键盘方法汇总

    在iOS应用开发中,有三类视图对象会打开虚拟键盘,进行输入操作,但如何关闭虚拟键盘,却没有提供自动化的方法。这个需要我们自己去实现。这三类视图对象分别是UITextField,UITextView和UISearchBar。 这里介绍一下...

    代码详解ios键盘收起问题

    在开发过程中,我们经常会用到UITextField、UITextView等文本框,然后这些文本框在点击之后会自动成为第一响应者(FirstResponder),并自动弹出软键盘。然而,没有自动定义好的软键盘的回收。今天,我在开发过程中...

    swift-cheat-sheet:这些是我经常查找或花了一些时间弄清楚的事情。 我希望他们也能帮助你

    快速备忘单 这些是我经常查找的内容或我花了一些时间的解决方案。...在 UITextView 中按回车键时关闭键盘 在 UITextView 中设置最大字符数 添加一个人到地址簿 对 UIColor 使用 HexColors 代码 设置和检索 NSUserDefau

    Quote:一个学习 Swift 的项目

    引用 学习内容: UIView 拍摄快照 UIActivityViewController 使用 UITextView 自动布局 segue.destinationViewController 关闭键盘 占位符模拟

    iPhone开发秘籍

    8.7 秘诀:关闭uitextview键盘 205 8.8 秘诀:向文本视图添加一个撤销(undo)按钮 207 8.9 秘诀:创建一个基于文本视图的html编辑器 209 8.10 秘诀:构建一个交互搜索栏 211 8.11 秘诀:添加标注(callout)...

    iPhone开发秘籍.part2.rar

    8.7 秘诀:关闭UITextView 键盘.....205 8.8 秘诀:向文本视图添加一个撤销 (Undo)按钮.....207 8.9 秘诀:创建一个基于文本视图的HTML 编辑器.....209 8.10 秘诀:构建一个交互搜索栏.....211 8.11 秘诀:添加...

    iPhone开发秘籍.part4.rar

    8.7 秘诀:关闭UITextView 键盘.....205 8.8 秘诀:向文本视图添加一个撤销 (Undo)按钮.....207 8.9 秘诀:创建一个基于文本视图的HTML 编辑器.....209 8.10 秘诀:构建一个交互搜索栏.....211 8.11 秘诀:添加...

    iPhone开发秘籍.part1.rar

    8.7 秘诀:关闭UITextView 键盘.....205 8.8 秘诀:向文本视图添加一个撤销 (Undo)按钮.....207 8.9 秘诀:创建一个基于文本视图的HTML 编辑器.....209 8.10 秘诀:构建一个交互搜索栏.....211 8.11 秘诀:添加...

Global site tag (gtag.js) - Google Analytics