1.键盘通知名都是系统自己定义好了的,不可改变。
UIKeyboardWillShowNotification
UIKeyboardWillHideNotification
UIKeyboardWillChangeFrameNotification
2.通知中传的字典类型参数
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey ="0.25"; 键盘弹起或隐藏动画时长
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}"; 键盘的bounds值
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 871.5}"; 键盘隐藏时中心位置
UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 600.5}"; 键盘弹起是中心位置
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 271}}"; 键盘隐藏时的frame值
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 465}, {414, 271}}"; 键盘弹起时的frame值
UIKeyboardIsLocalUserInfoKey = 1;
// ViewController.m
// Text_文本框通知
//
// Created by mncong on 15/12/4.
// Copyright © 2015年 mancong. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate>
{
UITextField *_tf;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
//获取系统发出的键盘弹起的通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//获取系统发出的键盘隐藏的通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//获取系统发出的键盘frame值改变的通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
//添加文本框
[self addTF];
}
#pragma mark 添加文本框
- (void)addTF
{
_tf = [[UITextField alloc] init];
_tf.translatesAutoresizingMaskIntoConstraints = NO;
_tf.delegate = self;
_tf.placeholder = @"这里就是万恶的被遮挡的文本框";
_tf.borderStyle = UITextBorderStyleLine;
[self.view addSubview:_tf];
//可以不必在意,我为了实验vfl语句能否获取控件的frame值。可以删掉这两行,再添加frame属性。
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_tf]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tf)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_tf(==40)]-100-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tf)]];
}
#pragma mark 键盘弹起
- (void)keyboardWillShow:(NSNotification *)noti
{
NSLog(@"键盘弹起");
NSDictionary * dict = noti.userInfo;
NSLog(@"%@",dict);
/**dict:
{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 871.5}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 600.5}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 271}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 465}, {414, 271}}";
UIKeyboardIsLocalUserInfoKey = 1;
}
*/
//获取文本框的最大Y值。
CGFloat tfMaxY = CGRectGetMaxY(_tf.frame);
//获取键盘弹起之后的Y值。
CGFloat keyboardY = [dict[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
//获取键盘弹起使用的时间。
CGFloat duration = [dict[UIKeyboardAnimationDurationUserInfoKey] floatValue];
//判断是否遮挡
if (tfMaxY > keyboardY) {
//如果遮挡,添加动画,改变view的transform。
[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, keyboardY-tfMaxY);
}];
}
}
#pragma mark 键盘隐藏
- (void)keyboardWillHide:(NSNotification *)noti
{
//获取隐藏时间
CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
//取消transform的改变量。
[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
#pragma mark frame改变的通知
- (void)keyboardFrameChange:(NSNotification *)noti
{
NSLog(@"键盘frame改变了");
}
#pragma mark 键盘的代理方法 点击return
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步