【代码笔记】iOS-键盘自适应弹出
一,效果图。

二,工程图。

三,代码。
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-键盘自适应弹出的更多相关文章
- 【代码笔记】iOS-自定义弹出框
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [s ...
- 【代码笔记】iOS-轮询弹出框
一,效果图. 二,工程图. 三,代码. RootViewController.m #import "RootViewController.h" //加入弹出框的头文件 #impor ...
- Android中检测软键盘的弹出和关闭
Android系统并没有提供明显的API来监听软键盘的弹出和关闭,但是在某些情况下我们还是有办法来检测软键盘的弹出和关闭. 从StackOverflow找到了一个不错的方法.但是这种只适用于在mani ...
- vue 监听手机键盘是否弹出及input是否聚焦成功
//定义移动端类型 function pageStats() { let u = navigator.userAgent, app = navigator.appVersion; let obj = ...
- iOS 键盘自适应(IQKeyboardManager)使用小结
IQKeyboardManager Github地址 经常在开发一个应用程序,我们遇到了一个问题,iPhone的键盘上滑覆盖的UITextField / UITextView.IQKeyboardMa ...
- 关于Android 打开新的Activity 虚拟键盘的弹出与不弹出
关于Android 打开新的Activity 虚拟键盘的弹出与不弹出 打开Activity 时 在相应的情况 弹出虚拟键盘 或者 隐藏虚拟键盘 会给用户非常好的用户体验 , 实现起来也比较简单 只需 ...
- 关于ios8模拟器不能输入中文问题以及软键盘不弹出问题
在Xcode菜单栏中 Product->scheme->Edit Scheme->Options->Application Region改为中国,就可以输入中文了, 如果软 ...
- Android 另类方法监听软键盘的弹出收起事件
http://www.cnblogs.com/csonezp/p/5065624.html 最近做的项目碰到个问题,a界面是fragment+recyclerview,b界面带个edittext,并且 ...
- Android: View换切后,无法正常设置焦点或切换后TextView的虚拟键盘不弹出
边学.边测试,花了三天时间完工一个小应用. 遇到很多问题,但最终还是解决了. 我的手机是Android2.2版,所以我也在是2.2版环境下学习,开发. 1. 在同一个Activity中的不同View( ...
- IOS项目之弹出动画终结篇
在之前写过IOS项目之弹出动画一.IOS项目之弹出动画二.IOS项目之弹出动画三,今天来一个终极封装已经上传到Github上弹出动画总结篇UIPopoverTableView. UIPopoverTa ...
随机推荐
- WCF:wsdl
- docker 下安装mssql-server-linux
docker search mssql 查找mssql镜像 docker pull microsoft/mssql-server-linux 拉去mssql镜像 docker images 查看镜像 ...
- Salt-API安装配置及使用
Python3使用saltstack和salt-api 安装python3 1. tar zxvf Python-3.5.1.tgz 2. cd Python-3.5.1 3. ./configure ...
- python并发编程之进程池,线程池concurrent.futures
进程池与线程池 在刚开始学多进程或多线程时,我们迫不及待地基于多进程或多线程实现并发的套接字通信,然而这种实现方式的致命缺陷是:服务的开启的进程数或线程数都会随着并发的客户端数目地增多而增多, 这会对 ...
- Oracle VM VirtualBox启动后莫名奇妙的报错
VirtualBox软件无法启动: 参考解决:http://blog.csdn.net/a_ssimi/article/details/52002939 修改兼容性:http://blog.csdn. ...
- oracle 异常关闭操作 导致数据库无法正常关闭 也无法启动
场景描述: 在关闭数据库的时候,命令没有打全,导致数据库没有正常关闭 解决办法: 重新建立个连接,然后切换到oracle用户 执行强制关闭数据库: OK 问题解决,不过生产环境 还是不推荐 shutd ...
- Delphi:程序自己删除自己,适用于任何windows版本(含源码)
Delphi:程序自己删除自己,适用于任何windows版本(含源码) function Suicide: Boolean; var sei: TSHELLEXECUTEINFO; szMod ...
- (转)X-Frame-Options响应头缺失漏洞
原文:https://blog.csdn.net/ljl890705/article/details/78071601 x-frame-options响应头缺失漏洞. 故名思意,就是返回的响应头信息中 ...
- (转)Python自动化运维之13、异常处理及反射(__import__,getattr,hasattr,setattr)
原文:http://www.cnblogs.com/xiaozhiqi/p/5778856.html https://blog.csdn.net/zong596568821xp/article/det ...
- 修复/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory问题
在配置MongDB的是时候出现这/lib/ld-linux.so.2问题 [root@localhost local]# /usr/local/mongodb/mongodb/bin/mongod - ...