• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIToolBar 在更改标签后恢复

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:28:52 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap