【问题标题】:UIToolBar reverts after changing a labelUIToolBar 在更改标签后恢复
【发布时间】:2015-04-04 02:46:54
【问题描述】:

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

当前,当我尝试更新标签计数器时,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 的看法是错误的。他们是下面的一个例子。底部的菜单挂起。我该如何设置它才不会发生?

【问题讨论】:

  • 这是因为自动布局。当您使用它时,您应该设置任何框架。您需要通过修改其约束来重新定位工具栏,而不是设置框架。
  • “这个问题没有得到足够的重视” 相反,@rdelmar 已经正确地告诉了你答案。问题是你没有在听。你无缘无故地丢掉你的名声。这个问题(关于更新文本字段时自动布局下会发生什么)在 Stack Overflow 上已经回答了很多次。
  • 我知道他的回复,这对我很有帮助,我非常感谢。如上所述,我已经编辑了问题以关注所提供信息的问题。我一直在寻找几个小时现在试图找到一个解决方案,但还没有找到它。因此,我提供了赏金。请随时链接一个问题和答案,以提供我需要的信息。
  • 为了解决这个问题,您能否提供一些代码来展示您如何定位/动画化这些 UI 元素?

标签: ios xcode uitoolbar


【解决方案1】:

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

  1. 为您的工具栏添加底部约束:

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

结果:

【讨论】:

    【解决方案2】:

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

    - (BOOL)canBecomeFirstResponder {
        return YES;
    }
    

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

    【讨论】:

      【解决方案3】:

      无需删除自动布局,只需在工具栏视图中添加两个约束尾随空间并修复宽度约束 希望这会帮助你我有类似的问题,我用这种方式解决了。

      【讨论】:

        【解决方案4】:

        您也可以通过设置框架在不自动布局的情况下做到这一点。在名为InputView 的视图中获取textField 和标签,并将其添加到self.view 和您的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。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-09-20
          • 2020-03-30
          • 1970-01-01
          • 2019-03-28
          • 1970-01-01
          • 2014-11-26
          • 2012-08-09
          相关资源
          最近更新 更多