一,效果图。

二,工程图。

三,代码。

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UITextFieldDelegate> @end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. UITextField *field=[[UITextField alloc]initWithFrame:CGRectMake(100, 300, 50, 50)];
field.backgroundColor=[UIColor redColor];
field.delegate=self;
[self.view addSubview:field]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification 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 -UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]; return YES;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

【代码笔记】iOS-键盘自适应弹出的更多相关文章

  1. 【代码笔记】iOS-自定义弹出框

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [s ...

  2. 【代码笔记】iOS-轮询弹出框

    一,效果图. 二,工程图. 三,代码. RootViewController.m #import "RootViewController.h" //加入弹出框的头文件 #impor ...

  3. Android中检测软键盘的弹出和关闭

    Android系统并没有提供明显的API来监听软键盘的弹出和关闭,但是在某些情况下我们还是有办法来检测软键盘的弹出和关闭. 从StackOverflow找到了一个不错的方法.但是这种只适用于在mani ...

  4. vue 监听手机键盘是否弹出及input是否聚焦成功

    //定义移动端类型 function pageStats() { let u = navigator.userAgent, app = navigator.appVersion; let obj = ...

  5. iOS 键盘自适应(IQKeyboardManager)使用小结

    IQKeyboardManager Github地址 经常在开发一个应用程序,我们遇到了一个问题,iPhone的键盘上滑覆盖的UITextField / UITextView.IQKeyboardMa ...

  6. 关于Android 打开新的Activity 虚拟键盘的弹出与不弹出

    关于Android 打开新的Activity 虚拟键盘的弹出与不弹出 打开Activity 时  在相应的情况 弹出虚拟键盘 或者 隐藏虚拟键盘 会给用户非常好的用户体验 , 实现起来也比较简单 只需 ...

  7. 关于ios8模拟器不能输入中文问题以及软键盘不弹出问题

    在Xcode菜单栏中   Product->scheme->Edit Scheme->Options->Application Region改为中国,就可以输入中文了, 如果软 ...

  8. Android 另类方法监听软键盘的弹出收起事件

    http://www.cnblogs.com/csonezp/p/5065624.html 最近做的项目碰到个问题,a界面是fragment+recyclerview,b界面带个edittext,并且 ...

  9. Android: View换切后,无法正常设置焦点或切换后TextView的虚拟键盘不弹出

    边学.边测试,花了三天时间完工一个小应用. 遇到很多问题,但最终还是解决了. 我的手机是Android2.2版,所以我也在是2.2版环境下学习,开发. 1. 在同一个Activity中的不同View( ...

  10. IOS项目之弹出动画终结篇

    在之前写过IOS项目之弹出动画一.IOS项目之弹出动画二.IOS项目之弹出动画三,今天来一个终极封装已经上传到Github上弹出动画总结篇UIPopoverTableView. UIPopoverTa ...

随机推荐

  1. web socket 入门

    WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议,其优雅地解决了以往web服务器不能向web客户端实时推送消息的问题. 在浏览器js环境中,创建一个websock ...

  2. 【xsy2193】Wallace 最大权闭合子图

    题目大意:给你一棵$n$个节点的树$a$,每个点有一个点权$val_i$,同时给你另一棵$n$个节点的树$b$. 现在你需要在树$a$上找一个联通块,满足这些点在树$b$上也是连通的,同时树$a$的这 ...

  3. odoo开发 相关知识点

    (1)导入模块可以起别名: (2) 新的模型前端要调用显示有关联的另一个模型的相关字段 (3) 传递上下文 搜索视图打开默认按照接收的参数搜索显示: 发起端视图 上下文写法: 目标端 触发显示,搜索视 ...

  4. sql server 2012 打开提示无效的许可证数据。需要重新安装

        重装什么的没有用,需要下载Visual Studio 2010 Isolated Shell (zh-CN) ,重新安装后就好了   下载地址 Visual Studio 2010 Isola ...

  5. 利用Warensoft Stock Service编写高频交易软件--客户端驱动接口说明

    Warensoft Stock Service Api客户端接口说明 Warensoft Stock Service Api Client Reference 本项目客户端驱动源码已经发布到GitHu ...

  6. 阿里Java开发规范&谷歌Java开发规范&华为Java开发规范&Tab键和空格比较&Eclipse的Tab键设置 总结

    现在收集到如下有用的信息: 阿里巴巴公开的Java开发规范:https://yq.aliyun.com/articles/69327?utm_content=m_10088 google公开的Java ...

  7. centos 7 hadoop的安装和使用

    准备工作 安装jdk 用户免密登录 安装参考文章: http://blog.csdn.net/circyo/article/details/46724335 http://www.linuxidc.c ...

  8. Apache Oltu 实现 OAuth2.0 服务端【授权码模式(Authorization Code)】

    要实现OAuth服务端,就得先理解客户端的调用流程,服务提供商实现可能也有些区别,实现OAuth服务端的方式很多,具体可能看 http://oauth.net/code/ 各语言的实现有(我使用了Ap ...

  9. es-09-spark集成

    es和spark的集成比较简单, 直接使用内部封装的一些方法即可 版本设置说明: https://www.elastic.co/guide/en/elasticsearch/hadoop/curren ...

  10. SQL 语句语法简介(一)

    语句分类 SQL 命令一般分为三类:DQL.DML.DDL. 一.DDL语句. 1.1建表语句 CREATE TABLE table_name( col01_name data_type, col02 ...