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

iOS 键盘显示事件处理

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

我正在开发一个聊天应用程序,其中我有一个 TextView (不是文本字段),当我点击它时,键盘应该显示并且所有内容都应该向上移动。

到目前为止,我已经设法将表格 View 和 TextView 的框架向上移动并使用以下代码显示键盘。

- (void)keyboardWasShownNSNotification *)notification {

    NSDictionary* info = [notification userInfo];

    keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGPoint contentViewOrigin = self.contentView.frame.origin;

    CGFloat contentViewHeight = self.contentView.frame.size.height;

    CGRect visibleRect = self.view.frame;

    visibleRect.size.height -= keyboardSize.height;
    BOOL up = CGRectContainsPoint(visibleRect, contentViewOrigin);

    if (!up){


    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,self.tableView.frame.origin.y,self.tableView.frame.size.width,280.0f);


    self.contentView.frame = CGRectOffset(self.contentView.frame, 0, 0 - keyboardSize.height);

    if([self.tableView numberOfRowsInSection:0]!=0)
    {
        NSIndexPath* ip = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:0]-1 inSection:0];
        [self.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:UITableViewRowAnimationLeft];
    }


}

}

- (void)keyboardWillBeHiddenNSNotification *)notification {

self.contentView.frame = originalContentView;
self.tableView.frame = originalTable;
}


- (void)registerForKeyboardNotifications {

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selectorselector(keyboardWasShown
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selectorselector(keyboardWillBeHidden
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

}

- (void)deregisterFromKeyboardNotifications {

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardDidHideNotification
                                              object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillHideNotification
                                              object:nil];

}

但是当我看到 whatsapp 是如何做到的时,我的看起来就像一个黑客。 Whatsapp 的键盘与所有元素一起向上移动,而我的工作方式如下:首先显示键盘,向应用发送通知,收到通知,代码计算键盘高度并根据高度向上移动元素。

我已经搜索并找到了我实现的解决方案。

有人可以帮忙吗?



Best Answer-推荐答案


我在我的应用程序中经常使用这个技巧。你想听 UIKeyboardWillShowNotificationUIKeyboardWillHideNotification。 在我看来,处理动画的最佳方式是使用自动布局。当你调用 [self.view layoutIfNeeded];您的 View 将与键盘动画一起移动。不需要动画 block 。

我设置了一个简单的 project任何人都可以尝试看看它是如何工作的!

- (void)addKeyboardNotificationsObserver {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selectorselector(handleKeyboardWillShow name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selectorselector(handleKeyboardWillHide name:UIKeyboardWillHideNotification object:nil];

}

- (void)handleKeyboardWillShowNSNotification *)paramNotification

{

    NSDictionary* info = [paramNotification userInfo];

    //when switching languages keyboard might change its height (emoji keyboard is higher than most keyboards). 
    //You can get both sizes of the previous keyboard and the new one from info dictionary. 

    // size of the keyb that is about to disappear
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    // size of the keyb that is about to appear
    CGSize kbSizeNew = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    //make adjustments to constraints here...

    //and here where's magick happen!

    [self.view layoutIfNeeded];

}

- (void)handleKeyboardWillHideNSNotification *)paramNotification

{
    //adjust constraints

    [self.view layoutIfNeeded];

}

关于iOS 键盘显示事件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30664160/

回复

使用道具 举报

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

本版积分规则

关注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