`

iPhone开发监听键盘状态、输入法状态

    博客分类:
  • ios
ios 
阅读更多
实现的功能:1)演示监听键盘状态(可解决键盘挡住输入法等问题)2)监听输入法状态

关键词:键盘状态 输入法状态 监听





1、新建一SingleView Application,命名为:KeyBoard&InputMethod,工程结果如下:
[img]

[/img]




2、修改ViewController.xib如下:
[img]

[/img]



3、ViewController.h不作修改,ViewController.m修改后如下:
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    //监听键盘状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    //监听输入法状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeInputMode:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
}

#pragma mark Notification
//keyBoard已经展示出来
- (void)keyboardDidShow:(NSNotification *)notification
{
    NSValue* aValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGRect keyboardFrame = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];
    CGFloat keyboardHeight = keyboardFrame.size.height;
    NSLog(@"##keboardHeight=%.2f",keyboardHeight);
}

//输入法发生切换
-(void)changeInputMode:(NSNotification *)notification{
    NSString *inputMethod = [[UITextInputMode currentInputMode] primaryLanguage];
    NSLog(@"inputMethod=%@",inputMethod);
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
  • 大小: 35.7 KB
  • 大小: 15.7 KB
分享到:
评论

相关推荐

    Android中底部菜单被输入法顶上去的解决方案

    在Android应用开发中,经常会遇到一个问题,即当用户在界面中触发软键盘输入时,底部菜单(如导航栏)会被输入法遮挡。这给用户体验带来了不便,因为底部菜单通常是重要的操作区域。本文将探讨几种解决这个问题的...

    jQuery可拖动多功能虚拟键盘插件

    在Web开发中,有时为了提供更好的用户体验,我们可能需要在网页上实现一个虚拟键盘,特别是在移动设备或触摸屏应用中。这个基于jQuery的虚拟键盘插件就是为此目的设计的。它具有丰富的功能,如可拖动、多语言支持...

    解决移动端 ios 系统键盘遮挡的问题

    一种常见的方法是监听键盘的显示和隐藏事件。在JavaScript中,可以使用`keyboardWillShow`和`keyboardWillHide`事件来获取键盘的状态。然而,这些事件在原生iOS环境中可用,而在Web应用中,我们需要利用一些技巧来...

    jQuery仿iPhone中文键盘插件

    在IT领域,jQuery仿iPhone中文键盘插件是一个实用的前端开发工具,主要用于为网页上的输入框提供模拟的、可拖动的虚拟键盘。这个插件的设计灵感来源于iPhone的原生键盘,旨在为用户在触屏设备上提供更好的输入体验。...

    自定义IOS控件

    对于键盘的交互,我们可以监听`UIKeyboardWillShowNotification`和`UIKeyboardWillHideNotification`通知,以便在系统键盘显示或隐藏时,调整自定义键盘的显示状态。同时,为了模仿系统键盘的动画效果,我们可以使用...

Global site tag (gtag.js) - Google Analytics