2

I've got a UIToolBar with a UITextField in it, along with a Label. I'm trying to get the label to update when the user types so they know how many characters they've typed.

Currently the UIToolBar returns to its original position when I try and update the label counter. Here is a gif showing the issue I'm having.

All I'm doing is the following:

-(IBAction)CharCount:(id)sender{
    NSString *substring = textField.text;
    NSString *limitHit;
    limitHit = substring;
    int maxChar = 160;
    if (limitHit.length > 0) {
        TextCounter.hidden = NO;
        TextCounter.text = [NSString stringWithFormat:@"%d/160", limitHit.length];
    }
}

How would I go about updating the label without reversing the animation to move the toolbar along with the keyboard?

======================== Edit ========================

Not using auto-layout means my view on an iPhone 4S is wrong. Their's an example below. The menu at the bottom hangs off. How do I set it so that doesn't happen?

example iPhone 4S

4
  • This is because of auto layout. When you're using that, you should not set any frames. You need to reposition the tool bar by modifying its constraints, instead of setting frames.
    – rdelmar
    Apr 4, 2015 at 4:51
  • 2
    "This question has not received enough attention" On the contrary, @rdelmar has correctly told you the answer. The problem is that you are not listening. You're throwing away your reputation for no reason. This question (about what happens under autolayout when a text field is updated) has been answered many times on Stack Overflow.
    – matt
    Apr 15, 2015 at 19:23
  • I'm aware of his response and it's helped a lot to which I'm very thankful. As mentioned above, I've edited the question to focus on the problem with the information provided. I've been searching for a few hours now trying to find a solution and have yet to find it. Hence why I offered the bounty. Please feel free to link a question and answer that will offer the information I need.
    – Jahoe
    Apr 15, 2015 at 19:36
  • 1
    In order to solve this, could you please provide some code showing how you position/animate these UI elements? Apr 15, 2015 at 21:03

4 Answers 4

1
+150

Don't turn off auto layout, just change constraints instead of frames. Changing frames with auto layout does not work because of layoutSubviews method. This method is called by system in many cases. You need:

  1. Add a bottom constraint to your toolbar:

    enter image description here

  2. Subscribe for keyboard notifications.

  3. Change bottom constraint of your toolbar when keyboard will show or hide.

Code sample:

- (void)dealloc {
    [self unsubscribeForKeyboardNotifications];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self subscribeForKeyboardNotifications];
}

#pragma mark - Keyboard notifications

- (void)subscribeForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillAppear:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillDisappear:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

}

- (void)unsubscribeForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillAppear:(NSNotification *)notification {
    CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    [self changeToolbarBottomConstraintWithConstant:keyboardHeight];
}

- (void)keyboardWillDisappear:(NSNotification *)notification {
    [self changeToolbarBottomConstraintWithConstant:0];
}

- (void)changeToolbarBottomConstraintWithConstant:(CGFloat)constant {
    [self.toolBar.superview.constraints enumerateObjectsUsingBlock:
            ^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
                if (constraint.secondItem == self.toolBar && constraint.secondAttribute == NSLayoutAttributeBottom)
                    constraint.constant = constant;
            }];
    [UIView animateWithDuration:0.5
                     animations:^{
                         [self.view layoutIfNeeded];
                     }];
}

Result:

enter image description here

1

Every part of this looks like it could be simplified and solved by setting the UIToolbar as the UITextview's inputAccessoryView. This will attach the toolbar to the keyboard as it animates up and down. If you want it to remain at the bottom of the view in the View Controller you can overwrite the inputAccessoryView of the View Controller and then add this method to your View Controller's implementation file:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

Here is a handy intro to using an inputAccessoryView on a view controller.

1

no need to remove autolayout just add two constraint trailing space to toolbarview and fix width constraint hope this will help you i have similar problem and i resolve with this way so.

1

You can do it without auto layout also by setting frames. Take textField and label in a view called InputView and add it in self.view and your textField as tfInput.

now set delegate for textfield in your view controller.

Then, Just change the Y position of view according to requirement.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
      if(textField== tfInput)
      {
           InputView.frame = CGRectMake(InputView.frame.origin.x,self.view.frame.size.height - 216 - InputView.frame.size.height,InputView.frame.size.width,InputView.frame.size.height);
      }
      return YES;

}

and

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
          if(textField== tfInput)
          {
               InputView.frame = CGRectMake(InputView.frame.origin.x,self.view.frame.size.height - 49 InputView.frame.size.height,InputView.frame.size.width,InputView.frame.size.height);
          }
          return YES;
}

here I set 49 as a toolbar size, it may be custom size by you. And also you can do some animation while frame set.

this is a one option by frame set.

second option is put it in a scrollview and in same textfield delegate method textFieldShouldBeginEditing you have to just set content offset to your needed place and make it 0 in textFieldShouldReturn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.