iOS-UITextField-通知
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;
// 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-通知的更多相关文章
- ios 消息通知
苹果的通知分为本地通知和远程通知,这里主要说的是远程通知 历史介绍 iOS 3 - 引入推送通知UIApplication 的 registerForRemoteNotificationTypes 与 ...
- iOS - Notification 通知
1.Notification 通知中心实际上是在程序内部提供了消息广播的一种机制,它允许我们在低程度耦合的情况下,满足控制器与一个任意的对象进行通信的目的.每一个 iOS 程序(即每一个进程)都有一个 ...
- IOS Notification 通知中心
1. 通知中心概述 通知中心实际上是在程序内部提供了消息广播的一种机制.通知中心不能在进程间进行通信.实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象. ...
- IOS开发-通知与消息机制
在多数移动应用中不论什么时候都仅仅能有一个应用程序处于活跃状态.假设其它应用此刻发生了一些用户感兴趣的那么通过通知机制就能够告诉用户此时发生的事情. iOS中通知机制又叫消息机制,其包含两类:一类是本 ...
- IOS 本地通知 UILocalNotification
IOS 本地通知 UILocalNotification [本文章第四部分中的代码逻辑来自网上的借鉴,并非我自己原创] 大概一个月前,我开始跟着做IOS项目了.学习C++,了解Objective-C, ...
- IOS中通知中心(NSNotificationCenter)
摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广. IOS中通知中心NSNotificationCenter应用总结 一.了 ...
- iOS 本地通知 操作
iOS 本地通知 操作 1:配置通知:然后退出程序: UILocalNotification *localNotif = [[UILocalNotification alloc] init]; loc ...
- 提高 iOS App 通知功能启用率的三个策略
我们都知道推送通知在 App 运营中的作用巨大.但是,很多用户却并不买帐,App 第一次启动提示是否「启用推送通知」时,他们直接选择了「否」. 是的,最近我本人就转变成了这样的人 - 认真地评估每个应 ...
- IOS NSNotification 通知
一. 先看下官方对NSNotification通知的解释 1. NSNotification 通知 @interface NSNotification : NSObject <NSCopying ...
- iOS - Push 通知推送
1.UserNotifications 通知是 App 用来和用户交流的一种方式,特别是当 App 并没有在前台运行的时候.通知,正如它的名称所强调的,被用作向用户'通知'一个事件,或者仅仅向用户提示 ...
随机推荐
- The main difference between Java & C++(转载)
转载自:http://stackoverflow.com/questions/9192309/the-main-difference-between-java-c C++ supports point ...
- gpg的一些常用操作
(1)列出keys # gpg --list-keys /root/.gnupg/pubring.gpg ------------------------ pub 2048R/98681A63 2 ...
- Windows下Nginx的安装与配置(转)
一.首先去官网下载 nginx1.0.11的Windows版本,官网下载:http://nginx.org/download/nginx-1.0.11.zip 下载到软件包后,解压 nginx-ngi ...
- Java 对文件的操作
public class ReadFile { /** * 按行读取文件操作 * @throws IOException */ public void readFile(String fileName ...
- OpenCV仿射变换+投射变换+单应性矩阵
本来想用单应性求解小规模运动的物体的位移,但是后来发现即使是很微小的位移也会带来超级大的误差甚至错误求解,看起来这个方法各种行不通,还是要匹配知道深度了以后才能从三维仿射变换来入手了,纠结~ esti ...
- asp.net 后台实现删除,划掉效果
效果: name = "<S>" + fircon + "</br>" + "</S>"; 增加“< ...
- Linux 求文件交集 差集等
使用comm命令 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d a FILE2内容如下: c d a c 基本上有两个方法,一个是comm命令,一个是g ...
- 【转】iOS设备的UDID是什么?苹果为什么拒绝获取iOS设备UDID的应用?如何替代UDID?
本文讲诉的主要是为什么苹果2011年8月发布iOS 5后就开始拒绝App获取设备的UDID以及UDID替补方案,特别提醒开发者苹果App Store禁止访问UDID的应用上架(相关推荐:APP被苹果A ...
- emoji和utf8mb4字符集
mysql 的 utf8 不支持 emoji,需要修改设置为utf8mb4 <?php 'mysql' => [ 'charset' => 'utf8mb4', 'collation ...
- ASP.NET 4.0 potentially dangerous Request.Form value was detected
A few days ago, while working on an ASP.NET 4.0 Web project, I got an issue. The issue was, when use ...