3 Replies
      Latest reply on Oct 11, 2017 4:37 PM by igorland
      igorland Level 1 Level 1 (0 points)

        Something crazy started happening in xCode 9. I use the following code to scroll the view when a keyboard is shown:

         

            func registerForKeyboardNotifications()
            {
                let notificationCenter = NotificationCenter.default
              
                notificationCenter.addObserver( self,
                                                selector: #selector(IGAFlightplanVC.keyboardWasShown(_:)),
                                                name: NSNotification.Name.UIKeyboardWillShow,
                                                object: nil )
              
                notificationCenter.addObserver( self,
                                                selector: #selector(IGAFlightplanVC.keyboardWillBeHidden(_:)),
                                                name: NSNotification.Name.UIKeyboardWillHide,
                                                object: nil)
            }
          
            func unregisterForKeyboardNotifications()
            {
                let notificationCenter = NotificationCenter.default
                notificationCenter.removeObserver(  self,
                                                    name: NSNotification.Name.UIKeyboardWillShow,
                                                    object: nil)
              
                notificationCenter.removeObserver(  self,
                                                    name: NSNotification.Name.UIKeyboardWillHide,
                                                    object: nil)
            }
        
          
            func keyboardWasShown(_ aNotification : NSNotification)
            {
                self.info = aNotification.userInfo! as NSDictionary!
              
                let value: NSValue = info.value(forKey: UIKeyboardFrameBeginUserInfoKey) as! NSValue
                let keyboardSize: CGSize = value.cgRectValue.size
              
                let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
        
                if self.scrollView != nil
                {
                    self.scrollView.contentInset = contentInsets
                    self.scrollView.scrollIndicatorInsets = contentInsets
        
                    var aRect: CGRect = self.view.frame
                    aRect.size.height -= keyboardSize.height
                  
                    if let field = self.activeField
                    {
                        let activeFieldRect: CGRect = field.frame
                        let activeFieldOrigin: CGPoint = activeFieldRect.origin
                        if !aRect.contains(activeFieldOrigin)
                        {
                            self.scrollView.scrollRectToVisible(activeFieldRect, animated:true)
                        }
                    }
                    else
                    {
                        print("Textfield is nil")
                    }
                }
            }
        
          
            func keyboardWillBeHidden(_ aNotification : NSNotification)
            {
                if self.scrollView != nil {self.scrollView.setContentOffset(CGPoint.zero, animated: true)}
            }
          
            func textFieldDidBeginEditing(_ theTextField : UITextField)
            {
                self.activeField = theTextField
              
                theTextField.text = ""
              
                let tapRecognizer = UITapGestureRecognizer(target: self,
                                                           action: #selector(tapDetected(_:)))
                if self.scrollView != nil {self.scrollView.addGestureRecognizer(tapRecognizer)}
            }
          
          
            func textFieldDidEndEditing(_ theTextField : UITextField)
            {
                self.activeField = nil;
            }
          
            func tapDetected(_ tapRecognizer: UITapGestureRecognizer)
            {
                self.activeField?.resignFirstResponder()
                if self.scrollView != nil {self.scrollView.removeGestureRecognizer(tapRecognizer)}
            }
        
        

         

        The scrollview moves up when I tap on the textfield and down when I tap on the view, BUT only twice or sometimes three times. After that, it stops moving the view. I have to reload the application to have it work, and it works again twice or thrice only and then stops moving the view. Never had the problem in xCode 8 before!

         

        Help would be much appreciated!

         

        EDIT: It appears to be a problem with iOS 11. It works fine on another iPad which I was smart enough not to update. Anyone has been in a similar situation?