一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

IOS开发 监听键盘状态、输入法状态及键盘输入法高度

时间:2015-01-16 编辑:简简单单 来源:一聚教程网

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

1)演示监听键盘状态(可解决键盘挡住输入法等问题)

2)监听输入法状态

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

2、修改ViewController.xib如下:

3、ViewController.h不作修改,ViewController.m修改后如下:

 

 代码如下 复制代码

#import "ViewController.h"

@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

 

 

ios 动态监听键盘输入法和高度

 

 代码如下 复制代码

//监听键盘高度变化

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasChange:) name:UIKeyboardDidChangeFrameNotification object:nil];

- (void)keyboardWasChange:(NSNotification *)aNotification {

    NSLog(@"Keyboard change");

    NSString *str=[[UITextInputMode currentInputMode] primaryLanguage];

    NSLog(@"shurufa--------------%@",str);

//    if ([str isEqualToString:@"zh-Hans"]) {

//        ReplayView.frame = CGRectMake(0, HEIGHT.height-216-125, 320, 45);

//    }else

//    {

//        ReplayView.frame = CGRectMake(0, HEIGHT.height-216-89, 320, 45);

//    }


    NSDictionary *info = [aNotification userInfo];

    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

   // CGRect frame = self.search.frame;

    if (kbSize.height == 216) {

        NSLog(@"english");

        ReplayView.frame = CGRectMake(0, HEIGHT.height-216-89, 320, 45);

    }

    else if(kbSize.height == 252){

        NSLog(@"中文");

        ReplayView.frame = CGRectMake(0, HEIGHT.height-216-125, 320, 45);

    }
}

 

热门栏目