[英]How to scroll UITextView above Keyboard in a UITableView in swift
我找到了用於Objective-C的解決方案: 在常規UIViewController上的UITableViewCell中,將UITextField滾動到Keyboard上方的鍵盤上,但是我無法使其適應快速變化。
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
var pointInTable:CGPoint? = textView.superview?.convertPoint(textView.frame.origin, toView: tableView)
var contentOffset:CGPoint = tableView.contentOffset
contentOffset.y = pointInTable?.y - textView.inputAccessoryView?.frame.size.height
}
我收到錯誤消息:
Value of optional type 'CGFloat?' not unwrapped; did you mean to use '!' or '?'?
當我使用! 要么 ? 我收到錯誤:后綴'?'的操作數 應該具有可選類型; 類型是“ CGFloat”
這是避免代碼中錯誤的一種方法:
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
var pointInTable:CGPoint = textView.superview!.convertPoint(textView.frame.origin, toView: tableView)
var contentOffset:CGPoint = tableView.contentOffset
contentOffset.y = pointInTable.y
if let accessoryView = textView.inputAccessoryView {
contentOffset.y -= accessoryView.frame.size.height
}
tableView.contentOffset = contentOffset
return true
}
首先,我們可以假設textView
具有一個superview
,因此我們可以用!
對其進行包裝!
。
結果, pointInTable
不再是可選的。
如果我們假設textView
可能沒有一個inputAccessoryView
,我們可以使用典型的if let
語法檢查inputAccessoryView
存在,如果有的話,減去其高度contentOffset.y
。
不要忘記為您的tableView
分配contentOffset
並返回一個Bool
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.