简体   繁体   English

Swift:键盘出现时滚动UITableView

[英]Swift: Scroll UITableView when keyboard appears

I've found answers to this, but nothing that I could get working for my specific case. 我已经找到了答案,但没有任何我可以为我的具体案例工作。

I have a UITableView with dynamic prototyped cells. 我有一个带动态原型单元格的UITableView One of these cells just contains a UITextView , and there is always only one of these and it is always at the very bottom. 其中一个单元格只包含一个UITextView ,并且总是只有其中一个,它始终位于最底层。 This is the only text view. 这是唯一的文本视图。

I can't connect this text view to an outlet in my controller because it throws an error (I'm assuming because there can be multiple instances of the text view, and they can't all be mapped to one outlet). 我无法将此文本视图连接到我的控制器中的插座,因为它会抛出错误(我假设因为文本视图可能有多个实例,并且它们不能全部映射到一个插座)。 So then I can't make my controller conform to the UITextView protocol. 那么我就不能让我的控制器符合UITextView协议。

Is there a simple and elegant solution to this? 有一个简单而优雅的解决方案吗?

try to observe the keyboard appearing events in your VC. 尝试观察VC中键盘出现的事件。 like this: 像这样:

NotificationCenter.default.addObserver(self, selector: #selector(scrollToTop(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(scrollToBottom(notification:)), name: .UIKeyboardWillHide, object: nil)

func scrollToTop(notification: NSNotification) {
        //change tableView insets
}


func scrollToBottom(notification: NSNotification) {
          //change tableView insets
 }

You can just set your controller as the textview's delegate in cellForRowAtIndexPath like this 您可以将控制器设置为cellForRowAtIndexPath中的textview委托,就像这样

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    ...
    cell.textView.delegate = self
    ...
    return cell
}

Actually your assumption that you cannot make your view controller to conform to UITextView protocol is wrong. 实际上你假设你不能让你的视图控制器符合UITextView协议是错误的。 You can use custom cell and add attach your outlet to a property in this custom cell. 您可以使用自定义单元格并将插座添加到此自定义单元格中的属性。 You can then assign tags to the text views and based on tags distinguish between various textViews. 然后,您可以为文本视图分配标签,并根据标签区分各种textView。

For setup of the delegate of textview in the ViewController you need to set it in the cellForRowAtIndexPath and you can access the delegate method of textview in your viewController. 要在ViewController中设置textview的委托,您需要在cellForRowAtIndexPath设置它,并且可以在viewController中访问textview的委托方法。

snippet with swift 3 is here swift 3的片段就在这里

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    cell.yourTextView.delegate = self //Assign your textView delegate here

}

For scrolling the tableview while keyboard appear/disappear is achieved by the NotificationCenter add the observer for keyboard appear/disappear on viewDidLoad as below 为了在键盘出现/消失时滚动tableviewNotificationCenter实现添加键盘的观察者在viewDidLoad上显示/消失,如下所示

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)

You can detect keyboard appear/disappear using below method 您可以使用以下方法检测键盘是否出现/消失

func keyboardWillShow(notification: NSNotification) {
     print((notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.size.height)
}


func keyboardWillHide(notification: NSNotification) {

 }

and don't forgot to remove Observer is disappear method of viewLifeCycle. 并且不要忘记删除Observer是viewLifeCycle的消失方法。

there are some third party libraries to achieve this here are some of them 有一些第三方图书馆可以实现这一目标

TPKeyboardAvoiding IQKeyboardManager [ Suggestion ] - when you are going to scroll the tableview up side make sure whatever you are insert into the table will be display in table view. TPKeyboardAvoiding IQKeyboardManager [ 建议 ] - 当您要向上滚动表格视图时,请确保在表格视图中显示插入表格的内容。 keep your tableview in such a way so that whenever you are enter any single content in tableview cell it must be visible by user. 以这样的方式保持您的tableview,以便无论何时在tableview单元格中输入任何单个内容,它都必须由用户可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM