Masonry 实现输入框随键盘位置改变
Github: https://github.com/saitjr/MasonryDemo
直接上代码:
#import "ViewController4.h"
#import "Masonry.h" @interface ViewController4 () @property (strong, nonatomic) UITextField *textField; @end @implementation ViewController4 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self];
} - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. _textField = [UITextField new];
_textField.backgroundColor = [UIColor redColor];
[self.view addSubview:_textField]; [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10);
make.centerX.equalTo(self.view);
make.bottom.mas_equalTo(0);
make.height.mas_equalTo(40);
}]; // 注册键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
} - (void)keyboardWillChangeFrameNotification:(NSNotification *)notification { // 获取键盘基本信息(动画时长与键盘高度)
NSDictionary *userInfo = [notification userInfo];
CGRect rect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(rect);
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改下边距约束
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-keyboardHeight);
}]; // 更新约束
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded];
}];
} - (void)keyboardWillHideNotification:(NSNotification *)notification { // 获得键盘动画时长
NSDictionary *userInfo = [notification userInfo];
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改为以前的约束(距下边距0)
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(0);
}]; // 更新约束
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded];
}];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.view endEditing:YES];
} @end
Masonry 实现输入框随键盘位置改变的更多相关文章
- 【转】swift实现ios类似微信输入框跟随键盘弹出的效果
swift实现ios类似微信输入框跟随键盘弹出的效果 为什么要做这个效果 在聊天app,例如微信中,你会注意到一个效果,就是在你点击输入框时输入框会跟随键盘一起向上弹出,当你点击其他地方时,输入框又会 ...
- swift实现ios类似微信输入框跟随键盘弹出的效果
封面(图文无关) 为什么要做这个效果 在聊天app,例如微信中,你会注意到一个效果,就是在你点击输入框时输入框会跟随键盘一起向上弹出,当你点击其他地方时,输入框又会跟随键盘一起向下收回,二者完全无缝连 ...
- 控制input输入框光标的位置
一:理解input, textarea元素在标准浏览器下两个属性selectionStart, selectionEnd. selectionStart: 该属性的含义是 选区开始的位置: selec ...
- h5 ios输入框与键盘 兼容性优化
起因 h5的输入框引起键盘导致体验不好,目前就算微信.知乎.百度等产品也没有很好的技术方案实现,尤其底部固定位置的输入框各种方案都用的前提下体验也并没有很好,这个问题也是老大难问题了.目前在准备一套与 ...
- 【转】IOS 输入框被键盘遮盖的解决方法
做IOS开发时,难免会遇到输入框被键盘遮掩的问题.上网上搜索了很多相关的解决方案,看了很多,但是由衷的觉得太麻烦了. 有的解决方案是将视图上的所有的东西都添加到一个滚动视图对象( UIScrollVi ...
- 移动端页面input输入框被键盘遮挡问题
<body class="layout-fixed"> <!-- fixed定位的头部 --> <header> </header> ...
- 表格头部与左侧内容随滚动条位置改变而改变(基于jQuery)
效果图如下: HTML代码: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta chars ...
- 【转】ios输入框被键盘挡住的解决办法
做IOS开发时,难免会遇到输入框被键盘遮掩的问题.上网上搜索了很多相关的解决方案,看了很多,但是由衷的觉得太麻烦了. 有的解决方案是将视图上的所有的东西都添加到一个滚动视图对象( UIScrollVi ...
- ios上有时候提交按钮点击两次才可以取消输入框软键盘
ios上有时候提交按钮点击两次才可以取消输入框软键盘,点击第一次软键盘消失,点击第二次输入框页面消失,这样用户体验不好.我的做法是用 touchstart 代替click来处理 反应快,但是有时候会出 ...
随机推荐
- python 捕获 shell 脚本的输出结果
import subprocessoutput =Popen(["mycmd","myarg"], stdout=PIPE).communicate()[0] ...
- 【转】Android横竖屏切换问题
Android横竖屏切换总结(Android资料) Android横竖屏要解决的问题应该就两个: 一.布局问题 二.重新载入问题 1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的 ...
- 基于Linux的oracle数据库管理 part3( 存储 网络 常用命令 )
主要内容 1. 常用存储模式 2. 配置网络 3. 维护指令 常用存储模式 - 文件系统 优点:管理方便, 缺点:读写慢 - 裸设备 只没有被格式化和挂载的磁盘, 只能有程序直接访问, 不能被操作系统 ...
- 彻底搞清javascript中this, constructor, prototype
说起这三个属性,肯定有一些同学和我一样,初学js时非常困惑,头大,一脸的迷茫.今天就来给大家彻底解决这些担心受怕的问题. 先看this this定义: this就是函数赖以执行的对象. 分析这句话: ...
- D:/apache2/conf/httpd.conf:Cannot load D:/apache2/modules/mod_actions.so
报错如下: errors reported here must be corrected before service can be started.httpd:Syntax error on lin ...
- js之dom_2
动态脚本1 载入脚本文件 var s = document.createElement("script"); s.src = "test2.js"; s.typ ...
- UVa 10954 (Huffman 优先队列) Add All
直接用一个优先队列去模拟Huffman树的建立过程. 每次取优先队列前两个数,然后累加其和,把这个和在放入到优先队列中去. #include <cstdio> #include <q ...
- BZOJ 1787 紧急集合
LCA.注意细节. #include<iostream> #include<cstdio> #include<cstring> #include<algori ...
- (转)每天一个Linux命令(8): tar
通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar命令可以为linux ...
- android动画学习
android动画学习 转载自:http://www.open-open.com/lib/view/open1329994048671.html 3.0以前,android支持两种动画模式,twe ...