簡體   English   中英

迅速出現鍵盤時滾動UITextView

[英]Scroll UITextView when keyboard appears in swift

我現在面臨的問題是UIScrollView中有一個UITextField。 當我單擊UITextField時,我正在使用UIToolBar顯示UIPickerView。 當PickerView出現時,我需要將UITextField向上移動。

這是我的設計 在此處輸入圖片說明

這是我的代碼

 func keyboardWillShow(notification: NSNotification)
{
    let userInfo = notification.userInfo
    if let info = userInfo
    {
        let animationDurationObject =
        info[UIKeyboardAnimationDurationUserInfoKey] as NSValue

        let keyboardEndRectObject =
        info[UIKeyboardFrameEndUserInfoKey] as NSValue

        var animationDuration = 0.0
        var keyboardEndRect = CGRectZero

        animationDurationObject.getValue(&animationDuration)
        keyboardEndRectObject.getValue(&keyboardEndRect)

        let window = UIApplication.sharedApplication().keyWindow

        keyboardEndRect = view.convertRect(keyboardEndRect, fromView: window)

        let intersectionOfKeyboardRectAndWindowRect =
        CGRectIntersection(view.frame, keyboardEndRect);

        UIView.animateWithDuration(animationDuration, animations: {[weak self] in

            self!.scrollView.contentInset = UIEdgeInsets(top: 0,
                left: 0,
                bottom: intersectionOfKeyboardRectAndWindowRect.size.height,
                right: 0)
            self?.scrollView.scrollRectToVisible(self!.activeTextField.frame, animated: true)
        })
    }
}

func keyboardWillHide(notification: NSNotification)
{
    let userInfo = notification.userInfo

    if let info = userInfo{
        let animationDurationObject =
        info[UIKeyboardAnimationDurationUserInfoKey]
            as NSValue

        var animationDuration = 0.0;

        animationDurationObject.getValue(&animationDuration)

        UIView.animateWithDuration(animationDuration, animations: {
            [weak self] in
            self?.scrollView.contentInset = UIEdgeInsetsZero
            self?.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero
        })
    }
}

func textFieldShouldReturn(textField: UITextField) -> Bool
{
    textField.resignFirstResponder()
    return true
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool
{
    var returnVal = true
    self.activeTextField = textField
    if textField == self.pickerTextField
    {
        returnVal = false
        var yval:CGFloat = self.view.bounds.size.height - 206
        self.pickerContainerView.frame = CGRectMake(0, yval, self.view.bounds.size.width, self.pickerContainerView.frame.size.height)
        self.pickerContainerView.hidden = false
        self.view.addSubview(self.pickerContainerView)
    }
    return returnVal
}

func textFieldDidEndEditing(textField: UITextField) {
    self.activeTextField = nil
    textField.resignFirstResponder()
}

當我單擊其他TextField時,出現鍵盤時這些TextField會上升。 但是為PickerTextField做什么

UItextView的任何想法

謝謝。

 You can try like below with textView delegate - 

 // MARK: UITextViewDelegate
    func textViewDidBeginEditing(textView: UITextView) {
        var scrollPoint : CGPoint = CGPointMake(0, self.textView.frame.origin.y)
        self.scrollView.setContentOffset(scrollPoint, animated: true)
    }

    func textViewDidEndEditing(textView: UITextView) {
        self.scrollView.setContentOffset(CGPointZero, animated: true)
    }

Swift 3版本:

func textViewDidBeginEditing(_ textView: UITextView) {
    let scrollPoint : CGPoint = CGPoint.init(x:0, y:textView.frame.origin.y)
    self.scrollView.setContentOffset(scrollPoint, animated: true)
}

func textViewDidEndEditing(_ textView: UITextView) {
    self.scrollView.setContentOffset(CGPoint.zero, animated: true)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM