UIToolBar在更改标签后恢复

UIToolBar在更改标签后恢复

问题描述:

我有一个带有UITextField的UIToolBar以及一个标签。我试图让用户输入时更新标签,以便知道他们键入了多少个字符。UIToolBar在更改标签后恢复

当我试着更新标签计数器时,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

+0

这是因为自动布局。当你使用它时,你应该不**设置任何框架。您需要通过修改其约束来重新定位工具栏,而不是设置框架。 – rdelmar 2015-04-04 04:51:22

+2

“这个问题还没有得到足够的重视”相反,@ rdelmar已经正确地告诉了你答案。问题是你没有听。你无缘无故地抛弃了你的名声。这个问题(关于更新文本字段时在autolayout下发生了什么)已经在Stack Overflow上多次得到解答。 – matt 2015-04-15 19:23:02

+0

我知道他的回应,并且我非常感谢他们。如上所述,我编辑了这个问题,重点关注所提供信息的问题。我一直在寻找几个小时试图找到解决方案,但还没有找到它。因此,我提供了赏金。请随时链接提供我需要的信息的问题和答案。 – Jahoe 2015-04-15 19:36:08

不要关闭自动布局,只需更改约束而不是帧。由于layoutSubviews方法的原因,使用自动布局更改框架不起作用。这种方法在很多情况下被系统调用。您需要:

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

    enter image description here

  2. 订阅键盘的通知。

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

代码示例:

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

这每一部分看起来像它可以简化并通过设置UIToolbar为解决UITextviewinputAccessoryView。这会在工具栏上下移动时将工具栏附加到键盘上。如果你想让它留在视图控制器视图的底部,您可以覆盖视图控制器的inputAccessoryView,然后将这个方法添加到您的视图控制器的实现文件:

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

Here是一个方便介绍在视图控制器上使用inputAccessoryView。

无需拆下自动布局仅仅增加两个约束尾随空间toolbarview和修复宽度约束 希望这将有助于你有类似的问题,我以这种方式解决。

您也可以通过设置框架而无需自动布局。在名为InputView的视图中添加textField和label并将其添加到自身中。查看和textField作为tfInput

现在在您的视图控制器中设置文本字段的委托。

然后,只需根据需要更改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。