خرید بک لینک

Vote count: 0

So, I’ve got a number of textfields in a scrollview which need to be scrolled up in order to not be covered by the keyboard.

My code to solve this:

    //MARK: - Keyboard Management Methods

func registerForKeyboardNotifications() {

    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self,
                                   selector: #selector(DetailViewController.keyboardWillBeShown(_:)),
                                   name: UIKeyboardWillShowNotification,
                                   object: nil)
    notificationCenter.addObserver(self,
                                   selector: #selector(DetailViewController.keyboardWillBeHidden(_:)),
                                   name: UIKeyboardWillHideNotification,
                                   object: nil)

}


func keyboardWillBeShown(sender: NSNotification) { // Called when the UIKeyboardDidShowNotification is sent
    let info: NSDictionary = sender.userInfo!
    let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as! NSValue
    let keyboardSize: CGSize = value.CGRectValue().size
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets

    // If active text field is hidden by keyboard, scroll it so it's visible
    var aRect: CGRect = self.view.frame
    aRect.size.height -= keyboardSize.height
    let activeTextFieldRect: CGRect? = activeTextField?.frame
    let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin
    if (!CGRectContainsPoint(aRect, activeTextFieldOrigin!)) {
        scrollView.scrollRectToVisible(activeTextFieldRect!, animated:true)
    }
}

// Called when the UIKeyboardWillHideNotification is sent
func keyboardWillBeHidden(sender: NSNotification) {
    let contentInsets: UIEdgeInsets = UIEdgeInsetsZero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}


//MARK: - UITextField Delegate Methods

func textFieldDidBeginEditing(textField: UITextField) {
    activeTextField = textField
    scrollView.scrollEnabled = true
}

func textFieldDidEndEditing(textField: UITextField) {
    activeTextField = nil
    scrollView.scrollEnabled = false
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.registerForKeyboardNotifications()
}

override func viewDidDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

Problem: The view doesn’t scroll back down after the keyboard is dismissed. Here is a quick video: http://f.cl.ly/items/3g1T241a1w0L033Y3j3k/AppBug.mov As you can see it is impossible to move the scroll view after the keyboard is dismissed as well.

asked 3 mins ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 27 فروردين 1395 ساعت: 18:40

صفحه بندی