Custom Views During Text Entry
The app I'm currently working on requires a custom view to be taking all the visible are between the search bar and the keyboard. So I spent a bit of time experimenting and came up with this little recipe that might be useful to you.
The first thing you need to do is register to be notified when the keyboard becomes active. This code makes that happen.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
I placed this code into my view controller's viewDidLoad method but a different location might be more appropriate for you.
Then in my keyboardWillShow: method I calculate the visible space and make that the frame of my overlay view. Like this;
- (void)keyboardWillShow:(NSNotification *)notification {
if(nil != self.view.window){
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
CGRect keyboardBounds = [[[notification userInfo] valueForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
CGRect searchBounds = [self.searchBar bounds];
CGFloat height = CGRectGetHeight(appFrame) - CGRectGetHeight(keyboardBounds) - CGRectGetHeight(searchBounds);
CGRect visibleBounds = CGRectMake(CGRectGetMinX(appFrame),
CGRectGetMinY(appFrame) + CGRectGetHeight(searchBounds),
CGRectGetWidth(appFrame),
height);
self.overlay.frame = visibleBounds;
}
}
This works for me because my overlay view becomes a subview of view controller's view. If your view hierarchy is different you might need to do some different math.
Now with this view in place I can process a single tap as a cancel. You could do just about anything here though.
Happy coding!





I did something very similar using NSNotification with selectedScope
http://blog.corywiles.com/using-nsnotificationcenter-with-uinavigation
Posted by Cory Wiles on October 10, 2009 at 09:06 AM MDT #
Quick check - keyboardWillShow is your own custom method, not one of Cocoa's?
Posted by Patrick on October 10, 2009 at 09:06 AM MDT #
@Patrick - yep, its my own method, just need to follow the conventions for the notification center.
Posted by Bill Dudney on October 12, 2009 at 06:14 AM MDT #