开发者

Question about passing a NSNotification for scrolling a view when keyboard shows

开发者 https://www.devze.com 2023-02-02 21:41 出处:网络
I have a view with five UITextFields spaced from top to bottom. When you click in a text field the keyboard pops up and covers the bottom two text fields. I have tweaked the code to the point where if

I have a view with five UITextFields spaced from top to bottom. When you click in a text field the keyboard pops up and covers the bottom two text fields. I have tweaked the code to the point where if you click in one of the bottom two text fields the view will scroll up so that text field is visible.

The problem I am having is when you start in the first text field and tab through the text fields the view does not scroll when you get to the last two text field. I have determined this is because when tabbing between views my method keyboardWillShow never gets called again after we have clicked in the first text Field.

I have tried using the textFieldDidBeginEditing method to determine which text field now has focus and then I wanted to call my keyboardWillShow method again. But that method takes a NSNotification as an argument and through all of my search I have seen that you never really want to create an NSNotification object and instead want to use postNotificationName from NSNotificationCenter to pass a NSNotification object. I have been unable to get this to work properly.

Here's the relevant code I have.

- (void) viewWillAppear:(BOOL)animated
{
  [[NSNotificationCenter defaultCenter] addObserver:self    selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window];
  [super viewWillAppear:animated];
}

- (void) viewWillDisappear:(BOOL)animated
{
 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
 [super viewWillDisappear:animated];
}

- (void) keyboardWillShow: (NSNotification *)notif
{
 // get our app delegate so we can access currentTextField
 ScrollingAppDelegate *appDelegate = (ScrollingAppDelegate *)[[UIApplication sharedApplication] delegate];
 appDelegate.notif = notif;
 NSDictionary *info = [notif userInfo];
 NSValue *aValue = [info objectForKey: UIKeyboardBoundsUserInfoKey];
 CGSize keyboardSize = [aValue CGRectValue].size;
 float bottomPoint = (appDelegate.currentTextField.frame.origin.y+ appDelegate.currentTextField.frame.size.height+10);
 scrollAmount = keyboardSize.height - (self.view.frame.size.height - bottomPoint);
 if (scrollAmount > 0) 
 {
  moveViewUp = YES;
  [self scrollTheView:YES];
 }
 else 
 {
  moveViewUp = NO;
 }
}

- (void)scrollTheView: (BOOL)movedUp 
{
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:0.3];
 CGRect rect = self.view.frame;
 if (movedUp) 
  {
  rect.origin.y -= scrollAmount;
 }
 else 
  {
  rect.origin.y += scrollAmount;
 }

 self.view.frame = rect;
 [UIView commitAnimations];
}


- (void) textFieldDidBeginEditing:(UITextField *)textField
{
 // declare app delegate so we can access a varibale in it.
 ScrollingAppDelegate *appDelegate = (ScrollingAppDelegate *)[[UIApplication sharedApplication] delegate];

 // set the app delegate variable currentTextField to the textField which jus开发者_如何学JAVAt got focus so we can access it
 // in our other method keyboardWillShow
 appDelegate.currentTextField = textField;
  // some how call keyboardWillShow here so the view will scroll to the current text field
}

If I am going about this all wrong please let me know. I have been searching the net for answers but the have eluded me so far. Everything I find about scrolling the view only handles one text field and not multiple the way I need it.

Thanks


I didn't bookmark the original post I found this in, but here's a link to the code snipet I've been using for exactly this kind of thing:

https://gist.github.com/295089

0

精彩评论

暂无评论...
验证码 换一张
取 消