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;

    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

  

时间: 2024-08-29 00:21:36

iOS对键盘的处理的相关文章

关闭ios虚拟键盘的几种方法

在iOS应用开发中,有三类视图对象会打开虚拟键盘,进行输入操作,但如何关闭虚拟键盘,却没有提供自动化的方法.这个需要我们自己去实现.这三类视图对象分别是UITextField,UITextView和UISearchBar. 这里介绍一下UITextField中关闭虚拟键盘的几种方法. 第一种方法,使用它的委托UITextFieldDelegate中的方法textFieldShouldReturn:来关闭虚拟键盘. 在UITextField视图对象如birdNameInput所在的类中实现这个方法

iOS 收起键盘的几种方式

iOS 收起键盘的几种方式 1.一般的view上收起键盘 // 手势 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.cellphone resignFirstResponder]; [self.password resignFirstResponder]; } 2.tableView 上收起键盘 // 通过滚动收起 -(void)scrollViewDidScroll:(UIScrollView *

IOS 隐藏键盘。

在View的UITextField中经常需要输入完文字后隐藏软键盘,要实现着一点要让View的Controller实现UITextFieldDelegate代理,然后编写相应的代码. #import <UIKit/UIKit.h> @interface TestVeiwController : UIViewController<UITextFieldDelegate> { IBOutlet UITextField *txt; } @property (nonatomic,retai

IOS 关闭键盘 退出键盘 的5种方式(转)

IOS 关闭键盘 退出键盘 的5种方式 转自“http://blog.csdn.net/w88193363/article/details/24423635” 分类: iphone2014-04-24 17:03 2197人阅读 评论(0) 收藏 举报 1.点击编辑区以外的地方(UIView) 2.点击编辑区域以外的地方(UIControl) 3.使用制作收起键盘的按钮 4.使用判断输入字元 5.关于键盘遮蔽的问题 1,点击编辑区以外的地方(UIView) 这是一种很直觉的方法,当不再需要使用虚

iOS获取键盘的高度(简洁有效)

iOS获取键盘的高度 (2013-03-06 17:45:31) 标签: keyboard textfield textview ios 键盘高度 分类: iOS笔记 - (void)viewDidLoad { [super viewDidLoad]; //增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKe

Can&#39;t find keyplane iOS模拟器键盘不显示解决办法

静下心来学技术,新一波的问题再度来袭 学习swift的时候做一个app,需要用到数字键盘,但是在调试了几次后数字键盘便出不来了,控制台中弹出这样一个错误 Can't find keyplane that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad; using 2382260317_PortraitChoco_iPhone-Simple-Pad_Default,查资料好像是xcode的一个bug,苹果居然放任如此严重的一

IOS: iPhone键盘通知与键盘定制

一.键盘通知 当文本View(如UITextField,UITextView,UIWebView内的输入框)进入编辑模式成为first responder时,系统会自动显示键盘.成为firstresponder可能由用户点击触发,也可向文本View发送becomeFirstResponder消息触发.当文本视图退出first responder时,键盘会消失.文本View退出first responder可能由用户点击键盘上的Done或Return键结束输入触发,也可向文本View发送resig

iOS 当键盘覆盖textFiled时简单的处理方法

//方法1--- - (void)textFieldDidBeginEditing:(UITextField *)textField { if (iPhone5) { return; } else { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; [UIView setAnimationDelegate:self]; //设定动画开始时的状态为目前画面上的样子 [UIView setAn

iOS给键盘添加控制栏

iOS中键盘的使用很频繁,有时给键盘上添加一个控制栏可以方便快捷的在不同输入框内进行切换或隐藏 这里简单说下具体实现方式 初始化一个UIToolBar并添加到界面,随着键盘的高度的更改而动态更改,从而进行展示 下面来看代码实现 头文件部分 #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface UIKeyboardTool : NSObject ///用于界面展示的toolbar @property

IOS 回收键盘通用代码

感觉IOS的键盘回收好累,所以封装了一个通用一点的方法 -(IBAction)spbResignFirstResponder:(id)sender { // NSLogObj(sender); if (sender == NULL || [sender isKindOfClass:[UITapGestureRecognizer class]]) { if (self.view != NULL && self.view.subviews != NULL && self.vie