二.处理文本框与键盘之间的关系(当键盘弹出遮挡到文本框的时候进行调整)
原理:
首先要明白:
1,键盘隐藏的时候,键盘上边界紧贴屏幕最低端,键盘在屏幕正下方。
2:键盘弹起的时候,键盘下边界跟屏幕下边界重合,键盘出现在屏幕最下方。
3:系统自带的键盘高度是固定的216,iOS8之后第三方的键盘,比如百度键盘高度可以任意调整。
4:当键盘弹起时,有可能会遮挡到文本框,也有可能遮挡不到。当遮挡到的时候需要处理,没遮挡的时候取消处理。
5:实现处理:(1):需要判断键盘弹起之后frame的Y值与文本框最大Y值的关系。(2):底层试图往上移动的距离 = 二者之间的差值(注意往上移动为负值)。
步骤:
1.接收系统发出的的三个通知
键盘弹起:获取两个Y值,即文本框的最大Y值和键盘弹起之后Y值。修改底层试图的transform。
键盘隐藏:取消对底层的transform的修改。
键盘frame值改变:可以获知改变的frame值,我暂时没有用到。
注意点:

1.键盘通知名都是系统自己定义好了的,不可改变。

UIKeyboardWillShowNotification

UIKeyboardWillHideNotification

UIKeyboardWillChangeFrameNotification

2.通知中传的字典类型参数

UIKeyboardAnimationCurveUserInfoKey = 7;

UIKeyboardAnimationDurationUserInfoKey ="0.25";                                    键盘弹起或隐藏动画时长

UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}";             键盘的bounds值

UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 871.5}";                 键盘隐藏时中心位置

UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 600.5}";                    键盘弹起是中心位置

UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 271}}";   键盘隐藏时的frame值

UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 465}, {414, 271}}";      键盘弹起时的frame值

UIKeyboardIsLocalUserInfoKey = 1;

// .m文件中的源码,可以拷贝观看

//  ViewController.m

//  Text_文本框通知

//

//  Created by mncong on 15/12/4.

//  Copyright © 2015年 mancong. All rights reserved.

//

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>

{

UITextField *_tf;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor grayColor];

//获取系统发出的键盘弹起的通知。

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

//获取系统发出的键盘隐藏的通知。

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

//获取系统发出的键盘frame值改变的通知。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

//添加文本框

[self addTF];

}

#pragma mark 添加文本框

- (void)addTF

{

_tf = [[UITextField alloc] init];

_tf.translatesAutoresizingMaskIntoConstraints = NO;

_tf.delegate = self;

_tf.placeholder = @"这里就是万恶的被遮挡的文本框";

_tf.borderStyle  = UITextBorderStyleLine;

[self.view addSubview:_tf];

//可以不必在意,我为了实验vfl语句能否获取控件的frame值。可以删掉这两行,再添加frame属性。

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_tf]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tf)]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_tf(==40)]-100-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tf)]];

}

#pragma mark 键盘弹起

- (void)keyboardWillShow:(NSNotification *)noti

{

NSLog(@"键盘弹起");

NSDictionary * dict = noti.userInfo;

NSLog(@"%@",dict);

/**dict:

{

UIKeyboardAnimationCurveUserInfoKey = 7;

UIKeyboardAnimationDurationUserInfoKey = "0.25";

UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}";

UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 871.5}";

UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 600.5}";

UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 271}}";

UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 465}, {414, 271}}";

UIKeyboardIsLocalUserInfoKey = 1;

}

*/

//获取文本框的最大Y值。

CGFloat tfMaxY = CGRectGetMaxY(_tf.frame);

//获取键盘弹起之后的Y值。

CGFloat keyboardY = [dict[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;

//获取键盘弹起使用的时间。

CGFloat duration = [dict[UIKeyboardAnimationDurationUserInfoKey] floatValue];

//判断是否遮挡

if (tfMaxY > keyboardY) {

//如果遮挡,添加动画,改变view的transform。

[UIView animateWithDuration:duration animations:^{

self.view.transform = CGAffineTransformMakeTranslation(0, keyboardY-tfMaxY);

}];

}

}

#pragma mark 键盘隐藏

- (void)keyboardWillHide:(NSNotification *)noti

{

//获取隐藏时间

CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

//取消transform的改变量。

[UIView animateWithDuration:duration animations:^{

self.view.transform = CGAffineTransformIdentity;

}];

}

#pragma mark frame改变的通知

- (void)keyboardFrameChange:(NSNotification *)noti

{

NSLog(@"键盘frame改变了");

}

#pragma mark 键盘的代理方法 点击return

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

[textField resignFirstResponder];

return YES;

}

@end

iOS-UITextField-通知的更多相关文章

  1. ios 消息通知

    苹果的通知分为本地通知和远程通知,这里主要说的是远程通知 历史介绍 iOS 3 - 引入推送通知UIApplication 的 registerForRemoteNotificationTypes 与 ...

  2. iOS - Notification 通知

    1.Notification 通知中心实际上是在程序内部提供了消息广播的一种机制,它允许我们在低程度耦合的情况下,满足控制器与一个任意的对象进行通信的目的.每一个 iOS 程序(即每一个进程)都有一个 ...

  3. IOS Notification 通知中心

    1.     通知中心概述 通知中心实际上是在程序内部提供了消息广播的一种机制.通知中心不能在进程间进行通信.实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象. ...

  4. IOS开发-通知与消息机制

    在多数移动应用中不论什么时候都仅仅能有一个应用程序处于活跃状态.假设其它应用此刻发生了一些用户感兴趣的那么通过通知机制就能够告诉用户此时发生的事情. iOS中通知机制又叫消息机制,其包含两类:一类是本 ...

  5. IOS 本地通知 UILocalNotification

    IOS 本地通知 UILocalNotification [本文章第四部分中的代码逻辑来自网上的借鉴,并非我自己原创] 大概一个月前,我开始跟着做IOS项目了.学习C++,了解Objective-C, ...

  6. IOS中通知中心(NSNotificationCenter)

    摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广.   IOS中通知中心NSNotificationCenter应用总结 一.了 ...

  7. iOS 本地通知 操作

    iOS 本地通知 操作 1:配置通知:然后退出程序: UILocalNotification *localNotif = [[UILocalNotification alloc] init]; loc ...

  8. 提高 iOS App 通知功能启用率的三个策略

    我们都知道推送通知在 App 运营中的作用巨大.但是,很多用户却并不买帐,App 第一次启动提示是否「启用推送通知」时,他们直接选择了「否」. 是的,最近我本人就转变成了这样的人 - 认真地评估每个应 ...

  9. IOS NSNotification 通知

    一. 先看下官方对NSNotification通知的解释 1. NSNotification 通知 @interface NSNotification : NSObject <NSCopying ...

  10. iOS - Push 通知推送

    1.UserNotifications 通知是 App 用来和用户交流的一种方式,特别是当 App 并没有在前台运行的时候.通知,正如它的名称所强调的,被用作向用户'通知'一个事件,或者仅仅向用户提示 ...

随机推荐

  1. 如何在windows7上安装启明星系统。

    启明星系统提供多种安装方式.安装包里自带了setup.exe.每个程序的 install下有在线安装(例如请假应用程序为book,则默认为 http://localhost/book/install ...

  2. centos下linux运行asp网站搭建配置-mono+nginx

    一.首先安装一些需要的软件包 1.  首先更新CentOS上的软件包:yum –y update. 2.  安装一些需要的库: yum -y install gcc gcc-c++ bison pkg ...

  3. (转)unity中基于alpha通道的shadow volume实现

    转自:http://blog.163.com/wmk_2000_ren/blog/static/138846192201019114117466/ 实现呢,Aras大神已经给出了, http://fo ...

  4. 分布式Hadoop安装(二)

    二.集群环境安装Zookeeper 1.         hadoop0,namenode机器下,配置zookeeper,先解压安装包. 使用命令:tar -zxvf zookeeper-3.4.4. ...

  5. 【转】Android类动态加载技术

    http://www.blogjava.net/zh-weir/archive/2011/10/29/362294.html Android应用开发在一般情况下,常规的开发方式和代码架构就能满足我们的 ...

  6. 汇编语言学习笔记(5)——[bx]和loop

    1.[bx]代表将bx寄存器中的值作为偏移地址. 2.loop与循环有关 3.inc bx的含义为bx中的内容+1 4.loop指令的格式为: loop 标号 CPU运行loop指令的时候.要进行两步 ...

  7. 【Android开发坑系列】之事件

    总结一下: 1.Touch事件分发中只有两个主角:ViewGroup和View.ViewGroup包含onInterceptTouchEvent.dispatchTouchEvent.onTouchE ...

  8. Linux2.6 内核的 Initrd 机制解析(转)

    from: https://www.ibm.com/developerworks/cn/linux/l-k26initrd/ 简介: Linux 的 initrd 技术是一个非常普遍使用的机制,lin ...

  9. Maven Learning - Direct Dependencies & Transitive Dependencies

    Dependencies declared in your project's pom.xml file often have their own dependencies. The main dep ...

  10. python数据结构之二叉树遍历的实现

    本篇是实现二叉树的三种遍历,先序遍历,中序遍历,后序遍历 #!/usr/bin/python # -*- coding: utf-8 -*- class TreeNode(object): def _ ...