ios - UIToolBar 更改标签后恢复

标签 ios xcode uitoolbar

我有一个 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 的看法是错误的。下面是他们的一个例子。底部的菜单悬空。我该如何设置才能避免这种情况发生?

example iPhone 4S

最佳答案

不要关闭自动布局,只需更改约束而不是框架。使用自动布局更改框架不起作用,因为 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

关于ios - UIToolBar 更改标签后恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29442180/

相关文章:

ios - 如何检测哪个 segue 标识符激活了当前 View Controller

iphone - 根据道路计算两个位置之间的距离

ios - 计算 UILabel 的一行可以容纳多少个字符

ios - react native : Dismiss Keyboard on scroll of Multiline TextInput

ios - 对至少未渲染过一次的 View (_UIReplicantView) 进行快照需要 afterScreenUpdates :YES

ios - 后栏按钮 Segue 隐藏工具栏

ios - 使带有 customView 的 UIBarButtonItem 表现得像 Flexible-Space item

ios - 为什么 Xcode 7 iOS 模拟器卡住?

ios - 如何在 Swift 中将 xib 的垂直长度设置为代码

iphone - 如何自定义QLPreviewController的navBar和toolbar tintColor