-
- (void)viewDidLoad
{
[super viewDidLoad]; //增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotifi cation object:nil]; //增加监听,当键退出时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotifi cation object:nil]; }
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度 NSDictionary *userInfo = [aNotification userInfo]; NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserIn foKey]; CGRect keyboardRect = [aValue CGRectValue]; float height = keyboardRect.size.height; }
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
}
iOS获取键盘的高度(在键盘出现,改变和消失时通知改变)
最新推荐文章于 2024-08-01 04:35:22 发布
本文介绍如何在iOS应用中监听键盘的弹出与隐藏事件,通过使用Notification Center来接收UIKeyboardWillShowNotification和UIKeyboardWillHideNotification通知,从而在键盘变化时进行相应的UI调整和布局优化。
502

被折叠的 条评论
为什么被折叠?



