2
votes

I have some problems trying to move the root view (with a scrollView) from my view controller when the textfield is hidden by the keyboard. This textfield is not in the root.

My app runs in iOS7 and has only Portrait mode, if the position is bottom home button I get the right values from UIKeyboardFrameEndUserInfoKey:

(CGRect) $2 = origin=(x=0, y=760) size=(width=768, height=264)

But if the position is Top home button y get a wrong origin from UIKeyboardFrameEndUserInfoKey:

(CGRect) $3 = origin=(x=0, y=0) size=(width=768, height=264)

Do you have any idea?

Thanks!

2
Has the app been configured to handle that orientation? - Mick MacCallum
Yes I have configured Portrait (top home button) and Portrait (bottom home button) in Supported interface orientations (iPad) - xarly

2 Answers

8
votes

I ran into this problem too. Although I feel like I never had to deal with this before, I can't say for sure since I haven't had too much experience dealing with this stuff. Although, it all works fine on the iPhone, so I'm not sure what's going on here.

Anyways, the apple documentation looks like this:

The key for an NSValue object containing a CGRect that identifies the end frame of the keyboard in screen coordinates. These coordinates do not take into account any rotation factors applied to the window’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow: method) or to view coordinates (using the convertRect:fromView: method) before using it.

So, I simply did:

keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

Which will convert the rect into the window's coordinate system.

2
votes

this is a documented feature:

Note: The rectangle contained in the UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey properties of the userInfo dictionary should be used only for the size information it contains. Do not use the origin of the rectangle (which is always {0.0, 0.0}) in rectangle-intersection operations. Because the keyboard is animated into position, the actual bounding rectangle of the keyboard changes over time.

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html