zoukankan      html  css  js  c++  java
  • 键盘问题

    iOS开 发中,对UITextField进行编辑的时候键盘会自己弹出来。在编辑完成的时候,需要将键盘隐藏掉。隐藏键盘有很多种实现方法,最常见的是把 TextField的firstResponder resign掉。即[textField resignFirstResponder]。本文介绍的是如何在键盘显示的时候,点击屏幕除了键盘以外的任何地方,将键盘隐藏

    基本思想如下:
    1. 在ViewController载入的时候,将键盘显示和消失的Notification添加到self.view里。
    2. 分别在键盘显示和消失时添加和删除TapGestureRecognizer

    示例代码如下:
    UIViewController的源代码里:

    (void)viewDidLoad
    {
        [super viewDidLoad];
      
      [self setUpForDismissKeyboard];
    }
    (void)setUpForDismissKeyboard {

     

      NSNotificationCenter *nc [NSNotificationCenter defaultCenter];
      UITapGestureRecognizer *singleTapGR =
      [[UITapGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(tapAnywhereToDismissKeyboard:)];
      NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
      [nc addObserverForName:UIKeyboardWillShowNotification
                      object:nil
                       queue:mainQuene
                  usingBlock:^(NSNotification *note){
                    [self.view addGestureRecognizer:singleTapGR];
                  }];
      [nc addObserverForName:UIKeyboardWillHideNotification
                      object:nil
                       queue:mainQuene
                  usingBlock:^(NSNotification *note){
                    [self.view removeGestureRecognizer:singleTapGR];
                  }];
    }

    (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //此method会将self.view里所有的subview的first responder都resign掉
      [self.view endEditing:YES];

    }

    梦想不是挂在嘴边炫耀的空气,而是需要认真的实践,等到对的风,我们展翅翱翔;没有风,只要拥有足够强壮的翅膀,我们照样拔地飞行。天空见。
  • 相关阅读:
    Delphi中常用字符串处理函数
    ListView的DrawSubItem时间添加边框,字体变粗问题
    解决d7在更高版本上运行乱码问题,或者是调用更高版本的dll
    使用Indy解决Could not load SSL Library错误
    局域网映射硬盘
    delphi http请求用到的编码方式
    delphi base64编码
    Java基础之抽象类
    ORA-12737: Instant Client Light: unsupported server character set CHS16GBK
    Android TagFlowLayout完全解析 一款针对Tag的布局
  • 原文地址:https://www.cnblogs.com/size/p/5908231.html
Copyright © 2011-2022 走看看