Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Have the keyboard slide into view from the right like when editing a note in Contacts

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?

like image 568
T . Avatar asked Jul 15 '09 17:07

T .


People also ask

What does character preview mean on iPhone?

Another keyboard feature you might not be aware of that can be changed is the functionality that lets you turn off what Apple calls "Character Preview." This is where the character "key" you type enlarges for a second as you type it, popping up on your screen. You can make those pop-ups go away.


2 Answers

Solution

In iOS 7, calling becomeFirstResponder on _textView in viewDidLayoutSubviews works.

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [_textView becomeFirstResponder];
}

Note: Doing it in viewWillLayoutSubviews also works.

Explanation

Read the discussion in the docs for becomeFirstResponder.

You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.

When using a navigation controller to push your custom view controller onscreen, self.view.window is still nil by the time either viewDidLoad or viewWillAppear: is called. So, _textView.window is also nil in the same methods, since _textView is a subview of self.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 also nil in initWithNibName:bundle:. self.view.window is set by the time viewDidAppear: 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 either viewWillLayoutSubviews or viewDidLayoutSubviews 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 and viewDidLayoutSubviews get called a lot more than just on the initial navigation controller push. But, navigationController:willShowViewController: and willMoveToParentViewController: get called too soon (after viewDidLoad but before self.view.window is set) and navigationController:didShowViewController: and didMoveToParentViewController: 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 since window is readonly.

like image 196
ma11hew28 Avatar answered Sep 19 '22 06:09

ma11hew28


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:

override func viewDidLoad() {
    super.viewDidLoad()
    someTextView.becomeFirstResponder()
}

This works in iOS 8. The keyboard slides in from the right along with the view.

like image 23
titaniumdecoy Avatar answered Sep 22 '22 06:09

titaniumdecoy