#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) UITextField *tf;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

_tf = [[UITextField alloc]initWithFrame:CGRectMake(10, self.view.bounds.size.height - 40, 300, 30)];

_tf.borderStyle = UITextBorderStyleRoundedRect;

[self.view addSubview:_tf];

/*

通知中心(单例)

通知中心是一对多的,即同一个广播可以被多个收音机接收

代理是一对一

作用:1.用来接收广播和发起广播

2.用通知的名字作为频道

*/

/*

参数一:响应的类

参数二:类中响应的方法

参数三:通知的名字(即频道)

参数四:接收类型 [注意]nil代表任何类型

*/

//:UIKeyboardWillShowNotification接收键盘将要显示的通知

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

//UIKeyboardWillHideNotification接收键盘将要隐藏的通知

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

}

#pragma mark - UIKeyboardWillHideNotification

//键盘将要隐藏

-(void)keyBoardWillHide:(NSNotification *)noti

{

//noti.userInfo是一个字典,大家可以输出来看看字典里面包含了什么

//NSLog(@"%@",noti.userInfo);

 

//获得弹下去后的坐标[注意是弹下后的]

CGRect keyBoardEndFrame = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

//NSLog(@"--%@",NSStringFromCGRect(keyBoardEndFrame));

[UIView animateWithDuration:2 animations:^{

//改变文本框的位置,让它跟着键盘一起弹起来

CGRect tfRext = _tf.frame;

tfRext.origin.y = keyBoardEndFrame.origin.y-_tf.bounds.size.height-10;

_tf.frame = tfRext;

}];

}

#pragma mark - UIKeyBoard notification

//接收到的是通知,所以参数用NSNotification

//键盘将要显示

-(void)keyBoardWillShow:(NSNotification *)noti

{

CGRect keyBoardFrame = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

//可以通过以下方法输出CGRect

//NSStringFromCGRect将CGRect转化为字符串的方式获取Frame

//NSLog(@"%@",NSStringFromCGRect(keyBoardFrame));

//获取键盘的动画持续时间

//CGFloat keyBoardDuration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

[UIView animateWithDuration:2 animations:^{

CGRect tfRect = _tf.frame;

tfRect.origin.y = keyBoardFrame.origin.y - _tf.bounds.size.height -10;

_tf.frame = tfRect;

}];

}

#pragma mark - 当手指触碰屏幕任何一地方时被调用

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

{

//利用结束编辑来隐藏键盘

[self.view endEditing:YES];

}

@end

UI键盘通知的更多相关文章

  1. AJ学IOS(18)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复

    AJ分享,必须精品 先看图片 第一步完成tableView和Cell的架子的图 完善图片 键盘弹出设置后图片: 自动回复图: 粗狂的架子 tableView和Cell的创建 首相tableView为了 ...

  2. IOS开发-键盘通知自定义键盘

    利用键盘通知可实现效果如下: 1.UIKeyboardWillShowNotification: 当点击到可输入的控件上(一般是UITextField)时,键盘会自动显示,并且触发UIKeyboard ...

  3. WPF 模拟UI 键盘录入

    原文:WPF 模拟UI 键盘录入 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/article/details/1835 ...

  4. ios-利用键盘通知处理键盘出现时遮挡控件问题

    -(void)viewDidLoad { NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; //注册键盘显示通知 ...

  5. 【代码笔记】iOS-只让textField使用键盘通知

    代码: #import "ViewController.h" @interface ViewController () @end @implementation ViewContr ...

  6. iOS开发——UI进阶篇(六)键盘处理

    一.键盘通知我们经常需要在键盘弹出或者隐藏的时候做一些特定的操作,因此需要监听键盘的状态 键盘状态改变的时候,系统会发出一些特定的通知UIKeyboardWillShowNotification // ...

  7. iOS开发——UI进阶篇(五)通知、代理、kvo的应用和对比,购物车

    一.通知 1.通知中心(NSNotificationCenter)每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信任何一个对象都可以向 ...

  8. iOS键盘监听的通知

    #pragma mark - 键盘通知回调方法 //  监听键盘的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector: ...

  9. iOS边练边学--通知机制和键盘处理

    一.通知中心(NSNotificationCenter) 每一个程序都有一个通知中心实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以想通知中心发布通知(NSNotification),描述 ...

随机推荐

  1. java并发编程读书笔记(1)-- 对象的共享

    1. 一些原则 RIM(Remote Method Invocation):远程方法调用 Race Condition:竞态条件 Servlet要满足多个线程的调用,必须是线程安全的 远程对象,即通过 ...

  2. maven基础知识

    1.maven基础知识 1.1maven坐标 maven坐标通常用冒号作为分割符来书写,像这样的格式:groupId:artifactId:packaging:version.项目包含了junit3. ...

  3. testing - 测试基本使用接口

    testing - 测试基本使用接口 当你写完一个函数,结构体,main之后,你下一步需要的就是测试了.testing包提供了很简单易用的测试包. 写一个基本的测试用例 测试文件的文件名需要以_tes ...

  4. WCF服务创建与使用(双工模式)

    昨天发布了<WCF服务创建与使用(请求应答模式)>,今天继续学习与强化在双工模式下WCF服务创建与使用,步骤与代码如下. 第一步,定义服务契约(Service Contract),注意Se ...

  5. 图论 --- spfa + 链式向前星 (模板题) dlut 1218 : 奇奇与变形金刚

    1218: 奇奇与变形金刚 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 130  Solved: 37[Submit][Status][Web Boa ...

  6. html的块级、内联、内联块级元素基础

    概念 块级:block 内联:inline 内联块级:inline-block 在html元素中,元素会有display属性 display属性默认值是block,那么该元素是块级元素. displa ...

  7. 只用CSS实现容器内图片上下左右居中

    一直以来,大家都知道,DIV容器内设置 text-align:center 即可让图片居中,但是DIV内默认的图片是上对齐,不会上下居中,如果想要实现这样的效果,JS判断是比较麻烦的,因为DIV容器内 ...

  8. C# winform OpenFileDialog MessageBox

    1.弹出窗体选择本地文件-OpenFileDialog OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Tit ...

  9. jquery取消超链接

  10. 孙鑫MFC学习笔记19:动态链接库

    1.动态链接库 2.静态库和动态库 3.使用动态链接库的好处 4.动态链接库编译需要导出,使用需要导入 __declspec(dllexport),__declspec(dllimport) 5.动态 ...