当键盘将其隐藏在swift中时,TableView单元格中的TextView不会向上移动

时间:2015-08-12 06:30:56

标签: ios swift

我正在尝试使用下面的代码在键盘隐藏时移动TextView。

override func viewDidLoad() {
        super.viewDidLoad()             

        registerForKeyboardNotifiactions()

        tableView.delegate = self
        tableView.dataSource = self

        tableView.estimatedRowHeight = 40
        tableView.rowHeight = UITableViewAutomaticDimension

    }

 var textsView = UITextView()

    func registerForKeyboardNotifiactions(){
        let ncUp = NSNotificationCenter.defaultCenter().addObserver(self, selector:     "keyboardWasShown:", name: UIKeyboardWillShowNotification, object: nil)
        var ncDown = NSNotificationCenter.defaultCenter().addObserver(self, selector:   "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
    }

    func keyboardWasShown(aNotification: NSNotification) {
        let info = aNotification.userInfo!

        let kbSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue().size
        let contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0)

        tableView.contentInset = contentInsets
        tableView.scrollIndicatorInsets = contentInsets

        var aRect = self.view.frame        
        aRect.size.height -= kbSize.height

        if !CGRectContainsPoint(aRect, textsField.frame.origin) {
            self.tableView.scrollRectToVisible(textsField.frame , animated: true)
        }

        else if !CGRectContainsPoint(aRect, textsView.frame.origin) {
            self.tableView.scrollRectToVisible(textsView.frame , animated: true)
        }
    }

    func keyboardWillBeHidden(aNotifiacation: NSNotification){
        let contentInsets = UIEdgeInsetsZero
        tableView.contentInset = contentInsets
        tableView.scrollIndicatorInsets = contentInsets
    }   

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

        let cells = tableView.dequeueReusableCellWithIdentifier("CommentCell", forIndexPath:    indexPath) as! MakeCommentCell

        self.textsView = cells.textView 
    cells.textView.delegate = self     
        textsField = cells.textField return cells 
    }

它可以与TextField一起使用,但是在TextView的情况下它不起作用。我尝试了许多改变底部限制和其他建议的东西,我可以在Stack OverFlow和其他网站找到但似乎没有一个对我有效(我可能没有清楚地理解它)。

我怎么能让它发挥作用?

如果有更好的方式我很想知道它。

谢谢

1 个答案:

答案 0 :(得分:0)

我认为代码中检查textField / textViews框架是否被键盘覆盖的部分有问题。

在比较CGGeometry结构之前,您应该确保它们来自相同的坐标空间。 textView的frame属性在它的superview的坐标空间上,我知道是表格单元格的contentView。您可能不应该直接将它与view.frame进行比较,而w / c位于view.superview的坐标空间。

UIView和UICoordinateSpace有转换这些结构的方法。

无论如何,这是我将如何进行检查:

let keyboardFrame = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()//the keyboard end frame on the coordinate space of the app's window
let textViewFrame = textView.convertRect(textView.bounds, toView: nil)//the textView frame on the coordinate space of the app's window
if CGRectIntersectsRect(keyboardFrame, textViewFrame) {
    let textViewFrame = textView.convertRect(textView.bounds, toView:self.tableView) //textView frame on the coord space of tableView
    self.tableView.scrollRectToVisible(textViewFrame , animated: true)
}
相关问题