zoukankan      html  css  js  c++  java
  • UItextView回收键盘的几种方式

    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:@" "]) {
    [textView resignFirstResponder];
    return NO;
    }
    return YES;
    }

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



    3.第三种方法感觉效果比上面两种都好,就是在弹出的键盘上面加一个view来放置退出键盘的Done按钮。

    代码如下:


    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleBlack];

    UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"Hello" style:UIBarButtonItemStyleBordered target:self action:nil];

    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];


    NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil];
    [doneButton release];
    [btnSpace release];
    [helloButton release];

    [topView setItems:buttonsArray];
    [tvTextView setInputAccessoryView:topView];

    -(IBAction)dismissKeyBoard
    {
    [tvTextView resignFirstResponder];
    }
  • 相关阅读:
    UIView的常见属性
    Object-C-Foundation-反射
    Object-C-自定义类型归档
    Java集合:HashMap源码剖析
    spring-boot-2.0.3之quartz集成,数据源问题,源码探究
    杂谈篇之我是怎么读源码的,授之以渔
    ElasticSearch 从零到入门
    java实现图片上传功能,并返回图片保存路径
    quartz定时任务及时间设置
    笛子初学者吹什么曲子
  • 原文地址:https://www.cnblogs.com/jx66/p/5173441.html
Copyright © 2011-2022 走看看