Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaving inputAccessoryView visible after keyboard is dismissed

What I'm trying to do is to create something similar to the "find on page" search function in Safari on iPad.

I'm using a UIToolbar with some items in it and attached it to the keyboard by setting it as an inputAccessoryView on the UITextField. Works like a charm, but there is one thing I can't figure out. In Safari, when you search for something, the keyboard disappears but the tool bar remains on the bottom of the screen.

Does anyone have a clue on how to accomplish this? The only solution I can think of is to respond to a keyboard dismissed event and then pull out the UIToolBar and create a custom animation that moves it to the bottom of the screen. But this is hacky. I am looking for a more elegant solution. Something that can make me decide what to do with the input accessory view when the keyboard gets dismissed.

like image 832
Tom van Zummeren Avatar asked May 26 '12 18:05

Tom van Zummeren


1 Answers

It's done like this:

Assign your UIToolbar to a property in your view controller:

@property (strong, nonatomic) UIToolbar *inputAccessoryToolbar; 

In your top view controller, add these methods:

- (BOOL)canBecomeFirstResponder{      return YES;  }  - (UIView *)inputAccessoryView{      return self.inputAccessoryToolbar;  } 

And then (optionally, as it usually shouldn't be necessary), whenever the keyboard gets hidden, just call:

[self becomeFirstResponder]; 

That way, your inputAccessoryToolbar will be both your view controller's and your text view's input accessory view.

like image 143
arik Avatar answered Sep 20 '22 15:09

arik