iOS - 通知监听键盘高度
2017-02-27 15:14
387 查看
#define SINGLE_NOTICE [NSNotificationCenter defaultCenter]
#import "ViewController.h"
@interface
ViewController ()<UITextFieldDelegate> {
float
_keyboardHeight; //
键盘高度
UITextField *_currentTF;
// 当前正在编辑的输入框
}
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated {
[super
viewDidAppear:animated];
[SINGLE_NOTICE
addObserver:self
selector:@selector(keyBoardWillShown:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
[super
viewDidDisappear:animated];
[SINGLE_NOTICE
removeObserver:self
name:UIKeyboardWillChangeFrameNotification
object:nil];
}
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor
= [UIColor whiteColor];
for (NSInteger i =
0; i<2; i++) {
// 方便对比,创建两个输入框
UITextField *tf = [[UITextField
alloc] init];
tf.frame =
CGRectMake(50,
500-300*i,
200, 40);
tf.backgroundColor = [UIColor
lightGrayColor];
tf.placeholder =
@"input your info at this...";
tf.delegate =
self;
tf.returnKeyType =
UIReturnKeyDone;
[tf setValue:[UIColor
redColor]
forKeyPath:@"_placeholderLabel.textColor"];
[self.view
addSubview:tf];
}
}
#pragma mark - 通知方法
- (void)keyBoardWillShown:(NSNotification *)noti {
NSDictionary *info = [noti
userInfo];
NSValue *value = [info
objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardRect = [value
CGRectValue];
if (_keyboardHeight ==
0.000000) {
_keyboardHeight = keyboardRect.size.height;
[self
makeTFVisible];
}
// NSLog(@"keyHeight===%f",_keyboardHeight);
_keyboardHeight = keyboardRect.size.height;
}
- (void)makeTFVisible {
float textFieldUpHeight =
_keyboardHeight - (self.view.frame.size.height
- (_currentTF.frame.size.height+_currentTF.frame.origin.y));
if (textFieldUpHeight >
0) {
[UIView
animateWithDuration:0.25
animations:^{
self.view.frame =
CGRectMake(0, -textFieldUpHeight,
self.view.frame.size.width,
self.view.frame.size.height);
}];
}
}
#pragma mark - 输入框代理方法 -
- (void)textFieldDidBeginEditing:(UITextField *)textField {
_currentTF = textField;
if (_keyboardHeight !=
0.000000) {
[self
makeTFVisible];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[UIView
animateWithDuration:0.25
animations:^{
self.view.frame
= CGRectMake(0,
0, self.view.frame.size.width,
self.view.frame.size.height);
}];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField.returnKeyType ==
UIReturnKeyDone) {
[textField resignFirstResponder];
}
return
YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches
withEvent:(UIEvent *)event {
[self.view
endEditing:YES];
}
#import "ViewController.h"
@interface
ViewController ()<UITextFieldDelegate> {
float
_keyboardHeight; //
键盘高度
UITextField *_currentTF;
// 当前正在编辑的输入框
}
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated {
[super
viewDidAppear:animated];
[SINGLE_NOTICE
addObserver:self
selector:@selector(keyBoardWillShown:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
[super
viewDidDisappear:animated];
[SINGLE_NOTICE
removeObserver:self
name:UIKeyboardWillChangeFrameNotification
object:nil];
}
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor
= [UIColor whiteColor];
for (NSInteger i =
0; i<2; i++) {
// 方便对比,创建两个输入框
UITextField *tf = [[UITextField
alloc] init];
tf.frame =
CGRectMake(50,
500-300*i,
200, 40);
tf.backgroundColor = [UIColor
lightGrayColor];
tf.placeholder =
@"input your info at this...";
tf.delegate =
self;
tf.returnKeyType =
UIReturnKeyDone;
[tf setValue:[UIColor
redColor]
forKeyPath:@"_placeholderLabel.textColor"];
[self.view
addSubview:tf];
}
}
#pragma mark - 通知方法
- (void)keyBoardWillShown:(NSNotification *)noti {
NSDictionary *info = [noti
userInfo];
NSValue *value = [info
objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardRect = [value
CGRectValue];
if (_keyboardHeight ==
0.000000) {
_keyboardHeight = keyboardRect.size.height;
[self
makeTFVisible];
}
// NSLog(@"keyHeight===%f",_keyboardHeight);
_keyboardHeight = keyboardRect.size.height;
}
- (void)makeTFVisible {
float textFieldUpHeight =
_keyboardHeight - (self.view.frame.size.height
- (_currentTF.frame.size.height+_currentTF.frame.origin.y));
if (textFieldUpHeight >
0) {
[UIView
animateWithDuration:0.25
animations:^{
self.view.frame =
CGRectMake(0, -textFieldUpHeight,
self.view.frame.size.width,
self.view.frame.size.height);
}];
}
}
#pragma mark - 输入框代理方法 -
- (void)textFieldDidBeginEditing:(UITextField *)textField {
_currentTF = textField;
if (_keyboardHeight !=
0.000000) {
[self
makeTFVisible];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[UIView
animateWithDuration:0.25
animations:^{
self.view.frame
= CGRectMake(0,
0, self.view.frame.size.width,
self.view.frame.size.height);
}];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField.returnKeyType ==
UIReturnKeyDone) {
[textField resignFirstResponder];
}
return
YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches
withEvent:(UIEvent *)event {
[self.view
endEditing:YES];
}
相关文章推荐
- iOS技巧——即时监听键盘高度
- iOS开发之监听键盘高度的变化
- ios 动态监听键盘输入法和高度
- iOS 监听键盘的高度 - NSNotificationCenter
- ios键盘高度监听
- iOS键盘中英文切换键盘高度获取通知方法
- IOS学习笔记(8)UIPageViewController UIProgressView 监听和响应键盘通知
- [iOS]利用通知实现监听系统键盘
- iOS 监听键盘高度,输入框上升
- iOS笔记监听键盘状态 获取键盘高度
- ios 键盘高度改变通知
- iOS 键盘显示或消失的监听,获取键盘高度
- ios-day08-02(ios的通知机制:通知的发布、监听、移除,一些常见的系统通知、键盘状态的通知、通知和代理的异同)
- ios 动态监听键盘输入法和高度
- ios 动态监听键盘输入法和高度
- iOS 评论界面 监听键盘高度 动态改变输入框位置
- iOS开发之监听键盘高度的变化
- 源码推荐(4.30):仿iOS简单计算器,简单涂鸦,自动监听键盘高度
- iOS-OC-监听键盘的高度 隐藏和出现
- [iOS]利用通知实现监听系统键盘