I am developing an iPad app that has a large number of UIViewControllers
, UITableViews
(with cells with accessoryViews
of UIText
I normally use the following
First:
Include the following extension in your implementation file, findFirstResponder
will help you find the first resonder
@implementation UIView (FindFirstResponder)
- (UIView*)findFirstResponder
{
if (self.isFirstResponder) {
return self;
}
for (UIView *subView in self.subviews) {
UIView *responder = [subView findFirstResponder];
if (responder)
return responder;
}
return nil;
}
@end
Then in your view controller viewDidLoad
add the following
- (void)viewDidLoad
{
[super viewDidLoad];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[center addObserver:self selector:@selector(keyboardDidHide:)
name:UIKeyboardWillHideNotification
object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
The notification functions will be like this
- (void) keyboardDidShow:(NSNotification*)notification
{
UIButton *button = [[UIButton alloc] init];
CGRect rect = self.view.bounds;
button.frame = rect;
button.backgroundColor = [UIColor blackColor];
button.tag = 111;
UIView *currentResponer = [self.view findFirstResponder];
[button addTarget:currentResponer action:@selector(resignFirstResponder) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:button belowSubview:currentResponer];
}
- (void) keyboardDidHide:(NSNotification*)notification
{
[[self.view viewWithTag:111] removeFromSuperview];
}
When the keyboard shows, i add a UIButton
beneath the current first responder, this button action will hide the keyboard,
A limitation here is that the UITextField
has to be in self.view
however you could adapt this technique for your need with some modifications, hope it helps you