Ios UIToolBar在更改标签后恢复

Ios UIToolBar在更改标签后恢复,ios,xcode,uitoolbar,Ios,Xcode,Uitoolbar,我有一个UIToolBar,里面有一个UITextField,还有一个标签。我试图让标签在用户键入时更新,以便他们知道键入了多少字符 当前,当我尝试更新标签计数器时,UIToolBar将返回其原始位置 我所做的只是以下几点: -(IBAction)CharCount:(id)sender{ NSString *substring = textField.text; NSString *limitHit; limitHit = substring; int maxC

我有一个UIToolBar,里面有一个UITextField,还有一个标签。我试图让标签在用户键入时更新,以便他们知道键入了多少字符

当前,当我尝试更新标签计数器时,UIToolBar将返回其原始位置

我所做的只是以下几点:

-(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];
    }
}
在不反转动画以随键盘移动工具栏的情况下,如何更新标签

==================================编辑========================

不使用自动布局意味着我对iPhone4S的看法是错误的。下面是他们的例子。底部的菜单挂起。我如何设置它,使其不会发生


通过将
UIToolbar
设置为
UITextview
inputAccessoryView
,似乎可以简化并解决此问题。这将在工具栏上下设置动画时将其附加到键盘上。如果希望它保留在视图控制器中视图的底部,可以覆盖视图控制器的
inputAccessoryView
,然后将此方法添加到视图控制器的实现文件中:

-(BOOL)可以成为第一响应者{
返回YES;
}

是在视图控制器上使用inputAccessoryView的简便介绍。

不要关闭自动布局,只需更改约束而不是框架。由于方法的原因,使用“自动布局”更改帧不起作用。在许多情况下,系统会调用此方法。你需要:

  • 将底部约束添加到工具栏:

  • 订阅键盘通知

  • 更改键盘显示或隐藏时工具栏的底部约束

  • 代码示例:

    - (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];
                         }];
    }
    
    结果:


    无需删除自动布局,只需在工具栏视图中添加两个约束尾部空间,并修复宽度约束即可
    希望这能帮助您解决我遇到的类似问题,这样我就可以解决。

    您也可以通过设置帧来实现无需自动布局。在名为
    InputView
    的视图中获取文本字段和标签,并将其添加到self.view中,然后将
    textField
    添加为
    tfInput

    现在在视图控制器中为textfield设置委托

    然后,只需根据需要更改视图的Y位置

    - (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;
    
    }
    

    在这里,我将49设置为工具栏大小,它可能是您自定义的大小。 另外,您还可以在设置帧时制作一些动画

    这是一个按帧设置的选项


    第二个选项是将其放在滚动视图中,并在相同的文本字段委托方法中
    textFieldShouldBeginEditing
    您只需将内容偏移量设置为所需位置,并在
    textFieldShouldReturn

    中使其为0,这是因为自动布局。在使用时,不应设置任何帧。您需要通过修改工具栏的约束来重新定位工具栏,而不是设置框架。相反,@rdelmar正确地告诉了您答案。问题是你没有在听。你在无缘无故地丢掉你的名声。这个问题(关于更新文本字段时在autolayout下会发生什么情况)在堆栈溢出问题上已经回答了很多次。我知道他的回答,这对我帮助很大,我非常感谢。如上所述,我已经编辑了这个问题,以便根据所提供的信息关注这个问题。我已经搜索了几个小时,试图找到一个解决方案,但还没有找到。因此,我提供了悬赏。请随意链接一个问题和答案,提供我需要的信息。为了解决这个问题,你能提供一些代码,显示你如何定位/动画这些UI元素吗?
    - (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;
    }