zoukankan      html  css  js  c++  java
  • iOS 退出键盘的方法

    键盘现在有个框架叫IQKeyboardManager十分吊, 如果你不想集成这个框架, 那么可以用我这个方法. 大体方式是: 放出全局通知, 监听键盘的弹出与退出, 在弹出时, 在键盘的上方加一个退出按钮, 全局只设置一次, 发一次通知就可以满足所有的textfiled, 具体代码如下:

    AppDelegate.h文件

     1 //
     2 //  AppDelegate.h
     3 //  HeHuJianKang
     4 //
     5 //  Created by zzt on 16/12/7.
     6 //  Copyright © 2016年 zzt. All rights reserved.
     7 //
     8 
     9 #import <UIKit/UIKit.h>
    10 
    11 @interface AppDelegate : UIResponder <UIApplicationDelegate>
    12 
    13 @property (strong, nonatomic) UIWindow *window;
    14 //点击退出键盘的按钮
    15 @property (nonatomic, weak)UIButton *closeKeyBoard;
    16 
    17 @end

    AppDelegate.m文件

     1 //
     2 //  AppDelegate.m
     3 //  HeHuJianKang
     4 //
     5 //  Created by zzt on 16/12/7.
     6 //  Copyright © 2016年 zzt. All rights reserved.
     7 //
     8 
     9 #import "AppDelegate.h"
    10 #import "ZTTabbarViewController.h"
    11 @interface AppDelegate ()    
    12 @end
    13 
    14 @implementation AppDelegate
    15     
    16 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    17    
    18     
    19     //设置根控制器
    20     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    21     ZTTabbarViewController *tab = [[ZTTabbarViewController alloc]init];
    22     self.window.rootViewController = tab;
    23     [self.window makeKeyAndVisible];
    24 
    25     //键盘退出按钮的添加, 全局的通知, 监测键盘的通知
    26     //添加自动退出键盘的按钮
    27     UIButton *closeKeyBoard = [[UIButton alloc]init];
    28     _closeKeyBoard = closeKeyBoard;
    29     _closeKeyBoard.frame = CGRectMake(0, MAXFLOAT, ZTScreenW, 30) ;
    30     //发出通知
    31     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    32     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
    33 
    34     return YES;
    35 }
    36 
    37 //实现通知的方法,键盘将要隐藏
    38 -(void)keyboardWillHidden:(NSNotification *)notification {
    39     [UIView animateWithDuration:1.0 animations:^{
    40         _closeKeyBoard.frame = CGRectMake(0, MAXFLOAT, ZTScreenW, _closeKeyBoard.zt_height);
    41     }];
    42 }
    43     
    44 //实现通知的方法,键盘将要显示
    45 - (void)keyboardWillShow:(NSNotification *)notification {
    46     //这样就拿到了键盘的位置大小信息frame,然后根据frame进行高度处理之类的信息
    47     CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    48     CGFloat keyboardH = frame.size.height;
    49     
    50     // 取得键盘的动画时间,这样可以在视图上移的时候更连贯
    51     double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    52     
    53     [UIView animateWithDuration:duration animations:^{
    54         UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    55         _closeKeyBoard.frame = CGRectMake(0, ZTScreenH - keyboardH - _closeKeyBoard.zt_height, ZTScreenW,_closeKeyBoard.zt_height);
    56         _closeKeyBoard.backgroundColor = [UIColor whiteColor];
    57         [_closeKeyBoard setBackgroundImage:[UIImage imageNamed:@"pack-up"] forState:UIControlStateNormal];
    58         [_closeKeyBoard setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    59         [window addSubview:_closeKeyBoard];
    60         //添加事件
    61         [_closeKeyBoard addTarget:self action:@selector(closeKeyboard) forControlEvents:UIControlEventTouchUpInside];
    62     }];
    63 }
    64     
    65 #pragma mark - 收起键盘
    66 - (void)closeKeyboard {
    67     //[self.searchBar resignFirstResponder];
    68     [self.window endEditing:YES];
    69 }

    就可以了.

    但是遮挡问题自己还要在监听键盘计算键盘的高度之后, 再确定textfiled的位置之后, 再相应修改view的Y值, 这样并不是一个好方法, IQKeyboardManager框架只要一句话都搞定了, 自己思量.

  • 相关阅读:
    Python-发送邮件
    Python基础-类的继承
    Python基础-列表推导式
    三、Linux下mysql的完整安装
    二、linux下apache2.2.11+php5.6.3的环境配置
    linux下编译安装php各种报错大集合
    一、linux下nginx1.7.8+php5.6.3的环境配置
    linux ./configure 的参数详解
    div随窗口变化设置高度
    在地图上增加标注点并为每个点增加各自的信息窗口
  • 原文地址:https://www.cnblogs.com/loveDodream-zzt/p/6439223.html
Copyright © 2011-2022 走看看