使用如下代码来调用键盘的高度。
每次重新下载APP之后的第一次,会出现yOffset被调用3次的情况,yOffset分别为 -184 -67.5 -67.5,以后就正常了,这个是怎么回事啊?效果如下图
– (void)regNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
– (void)unregNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark – notification handler
– (void)keyboardWillChangeFrame:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
每次重新下载APP之后的第一次,会出现yOffset被调用3次的情况,yOffset分别为 -184 -67.5 -67.5,以后就正常了,这个是怎么回事啊?效果如下图
– (void)regNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
– (void)unregNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark – notification handler
– (void)keyboardWillChangeFrame:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat yOffset = endKeyboardRect.origin.y – beginKeyboardRect.origin.y;
NSLog(@”yOffset:%f”,yOffset);
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + yOffset);
}
解决方案
40
这是在controller里码?不要改self.view的frame,在应该view里放一个容器,把其他的view添加进去,只调整容器的frame