objective-c – 键盘隐藏UITableView问题

我有一个视图控制器,我有一个表视图,在表视图中每行都有文本字段,当我点击文本字段键盘出现并隐藏表视图,然后看不到编辑,我该如何解决这个问题问题?我把观察者修复了视图控制器的对齐但是这里没有用的是代码..

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
 return YES;
}

-(void)keyboardWillShow {
// Animate the current view out of the way
[UIView animateWithDuration:0.3f animations:^ {
    self.viewFrame =  CGRectMake(0, -160, 320, 480);
}];
}

最佳答案

you have to write below code, it will hide and show keyboard.   

====>.h file: declare one UITextField as below:

UITextField *actifText;

====>.m file:

-(void)viewDidAppear:(BOOL)animated
{
  // Register notification when the keyboard will be show
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{            
[textField resignFirstResponder];
return YES;
}

-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void) keyboardWillShow:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

// Detect orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect frame = self.tblName.frame;

// Start animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];

// Reduce size of the Table view 
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    frame.size.height -= keyboardBounds.size.height;
else 
    frame.size.height -= keyboardBounds.size.width;

// Apply new size of table view
self.tblName.frame = frame;

// Scroll the table view to see the TextField just above the keyboard
if (self.actifText)
{
    CGRect textFieldRect = [self.tblName convertRect:self.actifText.bounds fromView:self.actifText];
    [self.tblName scrollRectToVisible:textFieldRect animated:NO];
}

[UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.actifText = nil;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
   self.actifText = textField;
}

-(void) keyboardWillHide:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

// Detect orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect frame = self.tblName.frame;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];

// Reduce size of the Table view 
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    frame.size.height += keyboardBounds.size.height;
else 
    frame.size.height += keyboardBounds.size.width;

// Apply new size of table view
self.tblName.frame = frame;

[UIView commitAnimations];
}
点赞