最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[基本解决]swift中当键盘出现时滚动列表视图到对应的区域(最底端)

Swift crifan 2756浏览 0评论

当出现键盘的时候,此处,目前是这样的:

即,还没实现:

将列表视图滚动到:

最底部

或者最好是:

滚动到 和键盘相关的区域

滚动到最底部是这样的:

swift uitableview scroll when keyboard show

swift scroll  uitableview  when keyboard

ios – How to scroll UITextView above Keyboard in a UITableView in swift – Stack Overflow

iPhone Development : Scroll UITextField above Keyboard in a UITableView OR UIScrollView in Swift and Objective C

swift uitableview  scroll  keyboard

Ray Wenderlich | Tutorials for Developers and Gamers

iOS – handle keyboard in UITableView in UIViewController | Js’ Technical Blog

试了半天,还是没找到好的解决办法。

最后用了:

滚动到最后一行,了事:

                //table view scroll
                //let convertedRect = messageTableView.convertRect(inputMessageTextField.bounds, fromView: inputMessageTextField)
//                let toolbarInChatViewRect = messageTableView.convertRect(inputMessageToolbar.bounds, fromView: inputMessageToolbar)
//                print("toolbarInChatViewRect=\(toolbarInChatViewRect)") //(0.0, 559.0, 375.0, 44.0)
//                messageTableView.scrollRectToVisible(toolbarInChatViewRect, animated: false)
                //                messageTableView.scrollRectToVisible(convertedKeyboardFrameEnd, animated: true)
               
//                let kbdFrame:CGRect = CGRectMake(
//                    0,
//                    convertedKeyboardFrameEnd.origin.y,
//                    convertedKeyboardFrameEnd.width,
//                    convertedKeyboardFrameEnd.height)
//                messageTableView.scrollRectToVisible(kbdFrame, animated: true)

//                messageTableView.scrollToNearestSelectedRowAtScrollPosition(UITableViewScrollPosition.Bottom, animated: true)
               
               
if messageList.count > 0 {
                   
messageTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: messageList.count 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Middle, animated: false)
                }

但是有个很不好的现象:

每次新增加cell后,都要刷新页面显示后,再定位到最后一行

-》显得每次都很乱。。。

但是目前没找到更好的办法,先凑合用吧。。。

UITableView.ScrollRectToVisible() doesn't scroll to bottom – uitableview

试了试:

                if messageList.count > 0 {
                   
let bottomCellIndexPath:NSIndexPath = NSIndexPath(forRow: messageList.count 1, inSection: 0)
                   
//                    messageTableView.scrollToRowAtIndexPath(bottomCellIndexPath, atScrollPosition: UITableViewScrollPosition.Middle, animated: false)
//                   
                    let bottomCell = messageTableView.cellForRowAtIndexPath(bottomCellIndexPath)!
                   
messageTableView.scrollRectToVisible(bottomCell.frame, animated: false)
                }

和上面也是同样的效果:

虽然可以滚动到最后,但是也是先刷新列表,然后再滚动,显得很乱的效果。

转载请注明:在路上 » [基本解决]swift中当键盘出现时滚动列表视图到对应的区域(最底端)

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.171 seconds, using 22.24MB memory