I'm looking for a way to slide the keyboard into view from the right, like what happens in the Contacts application when you edit a note.
My problem is that when I call [someTextView becomeFirstResponder] in viewWillAppear, the keyboard immediatly pops up with no animation. And when I call it in viewDidAppear, the view first slides in from the right (UINavigationController does the sliding), and then the keyboard slides in from the bottom.
Is it possible to have the keyboard slide in from the right, together with the view?
In iOS 7 (or any version before) you can make a simple thing in loadView, viewDidLoad or viewWillAppear
In this case you will get left-to-right appearance of the keyboard aligned with the motion of pushing view controller.
You could try sending the becomeFirstResponder message to the new view controller before you push it onto the stack. For example:
I have found that changing animations on things like they keyboard is pretty tough though, and if you read the Human Interface Guidelines Apple makes it pretty clear that they want certain things to act in certain ways, all the time. There are ways to change the behaviors of certain animations but they often involve undocumented API calls and are grounds for rejection from the app store. It would be a violation of HIG to have pushed views slide up from the bottom, for example.
Hope this helps.
All you need to do is tell the text view in question to become the first responder in the
viewDidLoad
method of the view controller you're pushing onto the navigation stack:This works in iOS 8. The keyboard slides in from the right along with the view.
-Import
UIResponder-KeyboardCache
to your project.-Add
[UIResponder cacheKeyboard:YES];
to theviewDidLoad
of the view before the keyboard view. It might be better to do this immediately when the application loads or during a time convenient when you can afford it (during an HTTP request, for example). In most cases, simply in the view before is sufficient.-Add the following to the
viewDidLoad
of the keyboard view.To explain, this will preload the keyboard view, which will remove the delay from the first call of the keyboard view. Calling
becomeFirstResponder
on the text field in the main queue causes it to slide in with the view instead of animating upward before the view slides in.Solution
In iOS 7, calling
becomeFirstResponder
on_textView
inviewDidLayoutSubviews
works.Note: Doing it in
viewWillLayoutSubviews
also works.Explanation
Read the discussion in the docs for becomeFirstResponder.
When using a navigation controller to push your custom view controller onscreen,
self.view.window
is stillnil
by the time eitherviewDidLoad
orviewWillAppear:
is called. So,_textView.window
is alsonil
in the same methods, since_textView
is a subview ofself.view
, i.e., they're both in the same window. No matter how you present your custom view controller,self.view.window
(and thus_textView.window
) is alsonil
ininitWithNibName:bundle:
.self.view.window
is set by the timeviewDidAppear:
is called, but that's too late because by that time, the navigation controller has already completed the animation of pushing the view onscreen.self.view.window
is also set by the time eitherviewWillLayoutSubviews
orviewDidLayoutSubviews
is called and these methods are called before the push animation of the navigation controller begins. So, that's why it works when you do it in either of those methods.Unfortunately,
viewWillLayoutSubviews
andviewDidLayoutSubviews
get called a lot more than just on the initial navigation controller push. But,navigationController:willShowViewController:
andwillMoveToParentViewController:
get called too soon (afterviewDidLoad
but beforeself.view.window
is set) andnavigationController:didShowViewController:
anddidMoveToParentViewController:
get called too late (after the push animation).The only other way I can think of doing it is to somehow observe the
window
property of_textView
so that you get notified when it changes, but I'm not sure how to do that sincewindow
isreadonly
.