1

I added a toolbar with TextField in NavigationController by

- (void)viewDidLoad
{
    // ...

    [self.navigationController setToolbarHidden:NO animated:YES];

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 2, 240, 36)];
    textField.placeholder = @"Leave your comment here...";
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    UIBarButtonItem *commentTextFieldBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:textField];

    self.toolbarItems = [NSArray arrayWithObject:commentTextFieldBarButtonItem];
}

When user click on the TextField, I hope the toolbar move up with keyboard. I tried this method. The toolbar moved up, but became empty. Am I doing something wrong? Please help me, thanks!!

Screenshot 1 - Before

Screenshot 2 - After

Community
  • 1
  • 1
iForests
  • 6,757
  • 10
  • 42
  • 75

1 Answers1

3

Best solution is to set inputAccessoryView to UITextField like this :

#pragma mark - UITextFieldDelegate

-(void)textFieldDidBeginEditing:(UITextField *)textField{
   // Call the createInputAccessoryView method we created earlier.
   // By doing that we will prepare the inputAccView.

   UIView *inputAccView = [self createInputAccessoryView];
      // Now add the view as an input accessory view to the selected textfield.
   [textField setInputAccessoryView:inputAccView];  
}

-(UIView *)createInputAccessoryView{
  // Create the view that will play the part of the input accessory view.
  // Note that the frame width (third value in the CGRectMake method)
  // should change accordingly in landscape orientation. But we don’t care
  // about that now.
  UIView *inputAccView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 40.0)];
  // Set the view’s background color. We’ ll set it here to gray. Use any color you want.
  [inputAccView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.20f]];
  // We can play a little with transparency as well using the Alpha property. Normally
  // you can leave it unchanged.
  [inputAccView setAlpha: 0.8];

  UIButton *btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
  [btnDone setFrame:CGRectMake(240.0, 6.0f, 70.0f, 28.0f)];
  [btnDone setTitle:@"Done" forState:UIControlStateNormal];
  [btnDone setBackgroundColor:[UIColor blackColor]];
  [btnDone.layer setCornerRadius:6.0f];
  [btnDone setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  [btnDone addTarget:self action:@selector(btnDoneForKeyboardAction:) forControlEvents:UIControlEventTouchUpInside];
  // Now that our buttons are ready we just have to add them to our view.
  [inputAccView addSubview:btnDone];

  return inputAccView;
}

Another solution:

When the view controller appears, we want to be notified of keyboard events so that we can show the toolbar in response to the keyboard appearing and disappearing:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

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

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

In response to the keyboard appearing, we can animate the toolbar up from the bottom of the screen:

- (void)keyboardWillShow:(NSNotification *)notification {
   //set your toolbar frame here for upper side
}

When the keyboard is dismissed, we do just the opposite:

- (void)keyboardWillHide:(NSNotification *)notification {
   //set your toolbar frame here for down side
}
Community
  • 1
  • 1
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • I used [this method](http://stackoverflow.com/questions/1951826/move-up-uitoolbar/2422116#2422116) to move my view, and it did move (second screenshot in my original post). But the toolbar is gone. It became an empty bar there. – iForests Aug 17 '12 at 11:20
  • 1
    Oh, I got it finally. I have to move self.view and the toolbar. [self.navigationController.toolbar setFrame:CGRectMake(self.navigationController.toolbar.frame.origin.x, self.navigationController.toolbar.frame.origin.y + (keyboardFrame.size.height * (up?-1:1)), self.navigationController.toolbar.frame.size.width, self.navigationController.toolbar.frame.size.height)]; – iForests Aug 17 '12 at 16:05