zoukankan      html  css  js  c++  java
  • iOS对键盘的处理

    方法1. 使用<UITextFeildDelegate>,使用的UITextField示例 设置其Delegate为self,点击return按钮隐藏键盘。实现函数如下:
       - (BOOL)textFieldShouldReturn:(UITextField *)textField  
       {   
             [textField resignFirstResponder];  
             return YES;
       }  
     
     
    方法2. 点击界面的其它空白地方隐藏
         由于UIViewController是继承自UIResponder的,所以可以覆写- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;这个开始触摸的方法来取消第一响应者,代码如下:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        [_tfPassword resignFirstResponder];

        [_tfUsername resignFirstResponder];

    }

     
    以上两种方法是初学iOS的时候使用的。
     
    3. 最好还是使用NotificationCenter的方法比较好,能获取键盘高度等详细信息
     
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UITextField *tfTest;
    @property (weak, nonatomic) IBOutlet UITextField *tfTest2;
    @end
    
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad]; 
    }
    
    - (void)viewDidappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
        //移除键盘监听消息
        [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotificationobject:nil];
        [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotificationobject:nil];
        //注册键盘监听消息     
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyShow:)name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyHide:)name:UIKeyboardWillHideNotification object:nil];
    }
     
    //弹出键盘消息响应
    - (void)keyShow:(NSNotification *)no
    {
        NSLog(@"keyShow");
    
        NSDictionary *dic = [no valueForKey:@"userInfo"];
        CGFloat heightKeyboard = [[dicvalueForKey:@"UIKeyboardBoundsUserInfoKey"]CGRectValue].size.height;
    /*
        NSDictionary *userInfo = [aNotification userInfo];
    
        NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
        CGRect keyboardRect = [aValue CGRectValue];
    
    */
        UIView *firstResponderView = [self getFirstResponderView];
        CGFloat distanceToBottom = self.view.frame.size.height -CGRectGetMaxY(firstResponderView.frame);
    
        //动画
        if(distanceToBottom < heightKeyboard){
            CGRect frame = self.view.frame;
            frame.origin.y = -(heightKeyboard - distanceToBottom);
            [UIView animateWithDuration:0.3f animations:^{
                [self.view setFrame:frame];
            } completion:^(BOOL finished) {
            }];
        }
    }
    
    //关闭键盘消息响应
    - (void)keyHide:(NSNotification *)no
    {
        NSLog(@"keyHide");
        CGRect frame = self.view.frame;
        frame.origin.y = 0;
    
        //动画
        [UIView animateWithDuration:0.3f animations:^{
            [self.view setFrame:frame];
        } completion:^(BOOL finished) {
        }];
    }
    
    //点击背景区域自动隐藏键盘
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        UIView *firstResponderView = [self getFirstResponderView];
        [firstResponderView resignFirstResponder];
    }
    
    //获取当前焦点所在的控件
    - (UIView *)getFirstResponderView{
        UIWindow *keyWindow = [[UIApplication sharedApplication]keyWindow];
        UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
        return firstResponder;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    @end
    
     
      
    
     
    

      

     

    PS://获取当前编辑框相对于self.view的位置

    CGRect frameRelative = [firstResponderView convertRect:firstResponderView.bounds toView:self.view];

     

  • 相关阅读:
    关于秒杀的系统架构优化思路
    如何设计一个秒杀系统
    RabittMQ实践(二): RabbitMQ 与spring、springmvc框架集成
    RabittMQ实践(一): RabbitMQ的安装、启动
    Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)
    Linux 网络 I/O 模型简介(图文)
    Java 网络编程(六) 使用无连接的数据报(UDP)进行通信
    Java 网络编程(五) 使用TCP/IP的套接字(Socket)进行通信
    Java 网络编程(四) InetAddress类
    Java 网络编程(三) 创建和使用URL访问网络上的资源
  • 原文地址:https://www.cnblogs.com/dongfangchun/p/5340553.html
Copyright © 2011-2022 走看看