2015年6月17日 星期三

調整scrollView的內容位置到目前的textView的位置

 func fitScrollView(shouldMove: Bool) {
        if let scrollView = scrollView {
            var frame = inputTextField.frame
            var newSize = inputTextField.sizeThatFits(CGSizeMake(frame.width, CGFloat.max))
            frame.size.height = inputTextField.contentSize.height
            textViewHeightConstraint.constant = newSize.height
            textViewWidthConstraint.constant = scrollView.frame.width - imageViewWidthConstraint.constant
           
            let cursorPosition = inputTextField.caretRectForPosition(inputTextField.selectedTextRange?.start).origin
            if cursorPosition.y.isInfinite {
                var offset = inputTextField.frame.origin.y - scrollView.frame.size.height + newSize.height
                if offset > 0 {
                    if shouldMove {
                        scrollView.setContentOffset(CGPointMake(0, offset), animated: true)
                    } else {
                        scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
                    }
                }
            }
        }
    }

沒有留言:

張貼留言