zoukankan      html  css  js  c++  java
  • UI-隐藏键盘

    键盘的出现于隐藏(代码实现)=================================

     1、通知案例:

    #import "ViewController.h"

    #import "UIView+FrameExtension.h" // 可以自己写,以后用着方便

    #define kDeviceHeight [UIScreen mainScreen].bounds.size.height

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        // 设置视图的背景色

        self.view.backgroundColor = [UIColor lightGrayColor];

        

        // 添加第一个文本框 假定位置

        UITextField *firstField = [[UITextField alloc]initWithFrame:CGRectMake(50, 300, 200, 40)];

        firstField.backgroundColor = [UIColor whiteColor];

        [self.view addSubview:firstField];

        

        // 添加第一个文本框

        UITextField *secondField = [[UITextField alloc]initWithFrame:CGRectMake(firstField.x, firstField.bottom + 50, firstField.width , firstField.height)];

        [self.view addSubview:secondField];

        secondField.backgroundColor = [UIColor whiteColor];

        

        // 注册键盘显示的通知

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];

        // 注册键盘隐藏的通知

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard: ) name:UIKeyboardWillHideNotification object:nil];

    }

    // 键盘弹出时执行这个方法,

    -(void)showKeyboard:(NSNotification *)notification{

        

        // 定义一个文本框,指向正在编辑的文本框,也就是弹出键盘的文本框

        UITextField *txtField;

        // 今次遍历当前视图的所有子视图, subViews数组保存的是当前视图所有的子视图

        for (UIView *subView in self.view.subviews) {

            // 如果这个子视图是一个文本框的话,isKindOfClass方法可以判断某个变量是不是某个类型的变量

            if ([subView isKindOfClass:[UITextField class]]) {

                // 先把这个子视图转化为文本框

                UITextField *tempField = (UITextField *)subView;

                // 再判断这个文本框是不是正在编辑

                if (tempField.isEditing ) {

                    // 如果这个文本框正在编辑,就是我要找的文本框,中断循环

                    txtField = tempField;

                    break;

                }

            }

        }

        

        NSLog(@"%@", notification);

        // 获取通知的userInfo属性

        NSDictionary *userInfoDict = notification.userInfo;

        // 通过键盘通知的userInfo属性获取键盘的bounds

        NSValue *value = [userInfoDict objectForKey:UIKeyboardBoundsUserInfoKey];

        // 键盘的大小

        CGSize keyboardSize = [value CGRectValue].size;

        // 键盘高度

        CGFloat keyboardHeight = keyboardSize.height;

        

        CGFloat offset = kDeviceHeight - keyboardHeight - txtField.bottom ;

        

        if (offset < 0 ) {      //这种情况下需要上移

            offset = offset - 10 ;     //保存上移的高度

            

            [UIView animateWithDuration:0.5 animations:^{

                

                self.view.transform = CGAffineTransformMakeTranslation(0, offset );

            }];

        }

        

    }

    -(void)hideKeyboard:(NSNotification *)notification{

        

        [UIView animateWithDuration:2 animations:^{

            

            self.view.transform = CGAffineTransformIdentity;

        }];

        

    }

       // 点击屏幕空白时隐藏键盘

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        [self.view endEditing:YES];

    }

    @end

    二、辞去第一响应者

    1、[self.view endEditing:YES];  //编辑结束 点击屏幕会退出键盘

    2、[firstField resignFirstResponder]; //辞去第一响应者

    3[firstField becomeFirstResponder]; //成为第一响应者

  • 相关阅读:
    atitit.交换机 汇聚上联、网络克隆和标准共享的原理与区别
    Atitit.数据库分区的设计 attilax  总结
    Atitit.数据库分区的设计 attilax  总结
    Atitit.常用分区api的attilax总结
    Atitit.常用分区api的attilax总结
    Atitit.  单列索引与多列索引 多个条件的查询原理与设计实现
    Atitit.  单列索引与多列索引 多个条件的查询原理与设计实现
    Atitit.sql where条件表达式的原理  attilax概括
    Atitit.sql where条件表达式的原理  attilax概括
    Atitit.分区对索引的影响 分区索引和全局索引 attilax总结
  • 原文地址:https://www.cnblogs.com/GJ-ios/p/5366469.html
Copyright © 2011-2022 走看看