UI Force the keyboard to go away
I'm making a little application to post photos to my photo blog. For the sake of learning I thought I'd make a little UI to go with it, but I've run into a problem.
I have made a sheet that has an ImageView at the top, with a button underneath to choose a photo from the camera roll. Below that, there are 3 TextFields for title and other stuff. I was quite proud of myself for creating a delegate class that shifts the whole View up when the text fields receive focus and then back down when they lose it! (I commented this all out to see if this was a problem but it still happens).
The ImageView, by chance, ends up being about as tall as the space left when the keyboard is visible, and here's where I hit a problem.
If I start the app, then press one of the text fields, the keyboard pops up. If I then press the 'choose image' button, I see the keyboard slide away as the photo picker slides in, but when I choose a photo, the ImageView updates and the keyboard slides back up but none of the text fields appear to be selected. Because the ImageView is the same size as the space above the keyboard, I can't select any of the other controls and can't dismiss the keyboard.
Is there a way to programmatically dismiss the keyboard so I could force it to go away when the 'pick photo' button is pressed?
Try putting all of the elements into a ScrollView. That would allow you to manually scroll up and down, and as far as I know ScrollViews automatically pseudo-resize when the keyboard is up, so there won't be any cases of controls getting "stuck" behind the keyboard.
end_editing()is the (secret) sauce.
Note that the keyboard frame modes are heavily bugged in version 1.5 in full screen mode for most rotations. (Anyone know if this was fixed in 1.6?). If you are using panel you should be fine.
If you need to use full screen for some reason,
This fixes the relevant conversion methods and keyboard frame methods.
Thanks guys. I'll try the ScrollView approach when I get some time and see where it gets me.
My somewhat clunky solution hides the keyboard on any attempt to scroll:
class MyScrollViewDelegate(object): def scrollview_did_scroll(self, scrollview): for sv in scrollview.subviews: if hasattr(sv, 'end_editing'): sv.end_editing()