OStack程序员社区-中国程序员成长平台

标题: ios - UIToolBar 在更改标签后恢复 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:28
标题: ios - UIToolBar 在更改标签后恢复

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

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

我正在做的事情如下:

-(IBAction)CharCountid)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



Best Answer-推荐答案


不要关闭自动布局,只需更改约束而不是框架。由于 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
                                             selectorselector(keyboardWillAppear
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selectorselector(keyboardWillDisappear
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

}

- (void)unsubscribeForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillAppearNSNotification *)notification {
    CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    [self changeToolbarBottomConstraintWithConstant:keyboardHeight];
}

- (void)keyboardWillDisappearNSNotification *)notification {
    [self changeToolbarBottomConstraintWithConstant:0];
}

- (void)changeToolbarBottomConstraintWithConstantCGFloat)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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4