更改标签后,UIToolBar会恢复

时间:2015-04-04 02:46:55

标签: ios xcode uitoolbar

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

当我尝试更新标签计数器时,UIToolBar返回其原始位置。 Here is a gif showing the issue I'm having.

我所做的就是以下几点:

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

如何在不反转动画的情况下更新标签,以便将工具栏与键盘一起移动?

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

不使用自动布局意味着我在iPhone 4S上的视图是错误的。他们是下面的一个例子。底部的菜单挂断了。如何设置它以便不会发生?

example iPhone 4S

4 个答案:

答案 0 :(得分:1)

通过将UIToolbar设置为UITextview的{​​{1}},可以简化和解决此问题的每个部分。这会将工具栏附加到键盘上,因为它可以向上和向下设置动画。如果您希望它保留在View Controller中视图的底部,您可以覆盖View Controller的inputAccessoryView,然后将此方法添加到View Controller的实现文件中:

inputAccessoryView

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

答案 1 :(得分:1)

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

  1. 向工具栏添加底部约束:

    enter image description here

  2. 订阅键盘通知。

  3. 当键盘显示或隐藏时,更改工具栏的底部约束。

  4. 代码示例:

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

    结果:

    enter image description here

答案 2 :(得分:1)

无需删除自动布局只需将两个约束尾随空格添加到工具栏视图并修复宽度约束 希望这会帮助你我有类似的问题,我这样解决。

答案 3 :(得分:1)

您也可以通过设置框架而无需自动布局。在名为InputView的视图中使用textField和label,并将其添加到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;

}

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

这里我将49设置为工具栏大小,它可能是您自定义的大小。 而且你也可以在框架集的同时做一些动画。

这是框架集的一个选项。

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

相关问题