我的代码:
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
float keyboardHight=keyboardRect.size.height;
}
我在这一行(float keyboardHight=keyboardRect.size.height;
)做一个断点,并打印这些变量和结果:
Printing description of userInfo:
{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 676}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 216}}";
UIKeyboardFrameChangedByUserInteraction = 0;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
}
Printing description of aValue:
NSRect: {{0, 568}, {320, 216}}
Printing description of keyboardRect:
(CGRect) keyboardRect = origin=(x=3.0491553029449835E-314, y=5.6817549271743353E-322) size=(width=3.0509261527954335E-314, height=3.0491551606540775E-314)
为什么[aValue CGRectValue]
会返回错误的结果。
它适用于iphone5c但不适用于iphone5s。任何想法都可以。感谢。