通过通知监听键盘的状态来改变View的位置
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>{
UIView * _mainView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 600)];
[self.view addSubview:_mainView];
_mainView.backgroundColor = [UIColor orangeColor];
// 新建一个UITextField,位置及背景颜色随意写的。
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];
textField.delegate = self;
textField.backgroundColor = [UIColor grayColor];
[self.view addSubview:textField]; // 自定义的view
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPosition:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPosition:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
// 根据键盘状态,调整_mainView的位置
- (void)changeContentViewPosition:(NSNotification *)notification{
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// 得到键盘弹出后的键盘视图所在y坐标
CGFloat keyBoardEndY = value.CGRectValue.origin.y;
// 键盘弹出所用时间
NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// 添加移动动画,使视图跟随键盘移动
[UIView animateWithDuration:duration.doubleValue animations:^{ [UIView setAnimationBeginsFromCurrentState:YES];
// 对View设置转场动画方向 枚举类型
[UIView setAnimationCurve:[curve intValue]];
_mainView.center = CGPointMake(_mainView.center.x, keyBoardEndY - 20 - _mainView.bounds.size.height/2.0); // keyBoardEndY的坐标包括了状态栏的高度,要减去
}];
}
-(void)dealloc{
//移除监听
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
@end
通过通知监听键盘的状态来改变View的位置的更多相关文章
- (二十四)监听键盘的通知和键盘弹出隐藏的View移动
让控制器监听键盘的通知,注意谁监听,谁的dealloc方法中就要remove,如果非ARC还要调用父类的dealloc方法. //监听键盘的操作: [[NSNotificationCenter def ...
- IOS 监听键盘的通知(NSNotificationCenter)
通知方法: /** * 当键盘改变了frame(位置和尺寸)的时候调用 */ - (void)keyboardWillChangeFrame:(NSNotification *)note { // 设 ...
- 为什么不要在viewDidLoad方法中设置开始监听键盘通知
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 一个普遍的错误是,程序猿(媛)试图在view controll ...
- Android 监听键盘的弹起与收缩
Android 监听键盘的弹起与收缩 由于android不存在该监听的API 所以需要自己去处理 先上代码 /* android:windowSoftInputMode="stateAlwa ...
- iOS实时监控网络状态的改变
在网络应用中,有的时候需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体 ...
- swift 监听键盘弹出的高度
// 监听键盘通知 NotificationCenter.default.addObserver(self, selector: #selector(ComposeViewController.key ...
- Android监听键盘右下角确定键
代码改变世界 Android监听键盘右下角确定键 kotlin写法 <EditText android:id="@+id/seachFriend" android:layou ...
- iOS--实时监控网络状态的改变
在网络应用中,有的时候需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体 ...
- 避免scrollview内部控件输入时被键盘遮挡,监听键盘弹起,配合做滚动
1,监听键盘 2,根据当前键盘弹起高度与控件的底部位置计算滑动距离 3,根据滑动距离在键盘弹起和隐藏是分别设置动画完成滑动 实现: 1,监听键盘使用 #pragma mark - 键盘监听 ...
随机推荐
- 怎么评价Facebook的Relay框架?Meteor.js 是什么?
http://www.zhihu.com/question/34531232?rf=34500201 Meteor.js 是什么? 作者:陈天链接:http://www.zhihu.com/quest ...
- WAP端 穿透问题和解决方法
1. 穿透问题可这么理解, 共有2种问题: 问题1: 有A 和 B 两个弹层,B 弹层盖在A 弹层上面,B 弹层绑定 touchend 事件,当用户点击B 的时候 B隐藏,由于touchend 事件触 ...
- eclipse:不能在tomcat里添加一个项目的解决方法
Cannot add a project to a tomcat server in eclipse You didn't create your project as "Dynamic W ...
- DGV表头双行
try { if (e.RowIndex < 0) { RectangleF _rect = e.CellBounds; Pen _pen = new Pen(Color.Black); Pen ...
- bootstrap--小李子demo
最近忙啊...看到各位冬鞋都在认真写博客,认真敲代码,认真工作,总觉得自己时间太少,总觉得时间不够,老了...... 进正题: 上次不知从哪里(忘了)下载了bootstrap的一些使用小demo,以后 ...
- Flex 对象克隆
package widget.EnvPlot{ public class copyObject extends Object { public function copyObjec ...
- 3.struts2接收页面传参的三种方式
Struts2通过拦截器机制封装了三种接收页面参数的方式: 1.属性驱动 2.模型驱动(有两种) Domain ModelDriven 1.属性驱动:这种方式比较简单,只要你直接在页面定义变量并且符合 ...
- window go protobuf
http://studygolang.com/articles/8804 protoc --go_out=. protocol.proto E:\TEST\TESTGRPC\src\google.go ...
- Device nodes and device stacks
[Device nodes and device stacks] 链接:https://msdn.microsoft.com/en-us/library/windows/hardware/ff5547 ...
- sass、less和stylus的安装使用和入门实践
刚 开始的时候,说实话,我很反感使用css预处理器这种新玩意的,因为其中涉及到了编程的东西,私以为很复杂,而且考虑到项目不是一天能够完成的,也很少是 一个人完成的,对于这种团队的项目开发,前端实践用c ...