iOS获取键盘的高度

来源:互联网 发布:java 广告位管理系统 编辑:程序博客网 时间:2024/05/16 01:06

利用观察者 来监听是否弹出键盘

//监听弹出键盘[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];//可以监听收回键盘[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];
//创建观察者回调方法- (void)keyboardWillShow:(NSNotification *)aNotification{    //创建自带来获取穿过来的对象的info配置信息    NSDictionary *userInfo = [aNotification userInfo];    //创建value来获取 userinfo里的键盘frame大小    NSValue *aValue = [userInfo         objectForKey:UIKeyboardFrameEndUserInfoKey];    //创建cgrect 来获取键盘的值    CGRect keyboardRect = [aValue CGRectValue];    //最后获取高度 宽度也是同理可以获取    int height = keyboardRect.size.height;    CGRect rect = self.view.frame;    rect.orignal.y = - height;    self.view.frame = rect;}- (void)keyboardWillHide:(NSNotification *)aNotification{    CGRect rect = self.view.frame;    rect.orignal.y = - height;    self.view.frame = rect;}
原创粉丝点击