代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. //为textField增加键盘事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addKeyboardNoti) name:UITextFieldTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeKeyboardNoti) name:UITextFieldTextDidEndEditingNotification object:nil]; }
#pragma -mark -keyboard notificatin
//键盘事件
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
// keyboardHeight 为键盘高度
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
[self animateViewWithKeyboardHeight:keyboardSize.height];
}
- (void)keyboardWillHide:(NSNotification *)notification {
[self animateViewWithKeyboardHeight:0.0];
}
- (void)animateViewWithKeyboardHeight:(CGFloat)keyboardHeight {
NSTimeInterval animationDuration = 0.3f;
CGFloat height = self.view.bounds.size.height;
CGFloat width = self.view.bounds.size.width;
CGFloat topSize = 0.0;
CGFloat viewH = self.view.frame.size.height-64;
CGFloat deviceHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat animateH = deviceHeight - viewH - keyboardHeight;
if (animateH >= 0) {
topSize = 0;
CGRect toRect = CGRectMake(0, topSize, width, height);
self.view.frame = toRect; } else {
topSize = animateH;
CGRect toRect = CGRectMake(0, topSize, width, height);
[UIView animateWithDuration:animationDuration animations:^{
self.view.frame = toRect;
}];
}
}
#pragma -mark -UITextFieldText Notification
//增加键盘事件
-(void)addKeyboardNoti
{
NSLog(@"------addKeyboardNoti-------");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//移除键盘事件
-(void)removeKeyboardNoti
{
NSLog(@"------removeKeyboardNoti---------");
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

【代码笔记】iOS-只让textField使用键盘通知的更多相关文章

  1. 【代码笔记】iOS-判断textField里面是否有空

    一,效果图. 二,工程图. 三,代码. ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional ...

  2. IOS TextField伴随键盘移动

    这篇文章介绍的是一个简单而又实用的小方法. 我想对于登陆时的一些效果大家应该都不会陌生. 今天就介绍一下,当开始输入TextField文本时键盘弹出TextField伴随键盘移动的实现. 先看一下演示 ...

  3. 【转】swift实现ios类似微信输入框跟随键盘弹出的效果

    swift实现ios类似微信输入框跟随键盘弹出的效果 为什么要做这个效果 在聊天app,例如微信中,你会注意到一个效果,就是在你点击输入框时输入框会跟随键盘一起向上弹出,当你点击其他地方时,输入框又会 ...

  4. IOS 开发之-- textfield和textview,return键的改变,点击return键

    IOS 开发之-- textfield和textview,return键的改变,点击return键 一,textfield的return键改变 方案1.改变键盘右下角的换行(enter)键为完成键,后 ...

  5. Flutter学习笔记(21)--TextField文本框组件和Card卡片组件

    如需转载,请注明出处:Flutter学习笔记(21)--TextField文本框组件和Card卡片组件 今天来学习下TextField文本框组件和Card卡片组件. 只要是应用程序就少不了交互,基本上 ...

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

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

  7. ios 弹出不同的键盘

    iOS 提供了10种键盘类型,在开发中,我们可以根据不同的需求,选择不同的键盘样式,例如,当我们只需要输入手机号码时,可以选择纯数字类型的键盘(NumbersAndPunctuation),当我们需要 ...

  8. 【hadoop代码笔记】Mapreduce shuffle过程之Map输出过程

    一.概要描述 shuffle是MapReduce的一个核心过程,因此没有在前面的MapReduce作业提交的过程中描述,而是单独拿出来比较详细的描述. 根据官方的流程图示如下: 本篇文章中只是想尝试从 ...

  9. 笔记-iOS 视图控制器转场详解(上)

    这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...

随机推荐

  1. postgresql-distinct on理解

    PostgreSQL 的 distinct on 的理解 对于 select distinct on , 可以利用下面的例子来理解: create table a6(id integer, name ...

  2. Salt-API安装配置及使用

    Python3使用saltstack和salt-api 安装python3 1. tar zxvf Python-3.5.1.tgz 2. cd Python-3.5.1 3. ./configure ...

  3. 关于AOP的PIAB

    AOP:面向切面编程,也就是在运行过程中,按照需要动态加载功能块 PIAB:含义可以从下面提取 Microsoft Patterns & Practices团队在2007年发布的Enterpr ...

  4. POJ 2591

    #include<iostream> #include<stdio.h> #define MAXN 10000001 using namespace std; int a[MA ...

  5. [翻译] Trident-ML:基于storm的实时在线机器学习库

    最近在看一些在线机器学习的东西,看到了trident-ml, 觉得比较有意思,就翻译了一下,方便有兴趣的读者学习. 本文为作者(掰棒子熊)翻译自https://github.com/pmerienne ...

  6. War文件部署

    其实,开始要求将源码压缩成War文件时,一头雾水! 公司项目要求做CAS SSO单点登录 也就是这玩意.... 其实war文件就是Java中web应用程序的打包.借用一个老兄的话,“当你一个web应用 ...

  7. 【Java初探03】——流程控制语句

    做任何事情都应当遵守一定的原则,程序设计也是如此,需要有流程控制语言来实现与用户的交流.流程控制对于任何一门编程语言来说都是至关重要的,它提供了控制程序步骤的基本手段,如果没有流程控制语句,整个程序将 ...

  8. Tomcat笔记:Tomcat的执行流程解析

    Bootstrap的启动 Bootstrap的main方法先new了一个自己的对象(Bootstrap),然后用该对象主要执行了四个方法: init(); setAwait(true); load(a ...

  9. 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)

    导读 1. 什么是序列化? 2. 为什么要序列化?好处在哪里? 3. C++对象序列化的四种方法 4. 最常用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序员在编写应用程序的时候往往需要将 ...

  10. Visual Studio 2017 取消 break mode

    用 Visual Studio 2017 (以下简称 VS 2017) 运行程序,程序出错后,只是进入中断模式,仅显示 The application is in break mode而没有像 VS ...