0

I have a method on a view controller which scrolls text fields up when focused if the keyboard is on screen.

I have tried to do the same thing on a different controller but with a textview but it's not working.

Attempt:

func keyboardOnScreen(notification: NSNotification){
        // Retrieve the size and top margin (inset is the fancy word used by Apple)
        // of the keyboard displayed.
        let info: NSDictionary  = notification.userInfo!
        let kbSize = info.valueForKey(UIKeyboardFrameEndUserInfoKey)?.CGRectValue.size
        let contentInsets: UIEdgeInsets  = UIEdgeInsetsMake(0.0, 0.0, kbSize!.height, 0.0)

        scrollView.contentInset = contentInsets
        scrollView.scrollIndicatorInsets = contentInsets

        var aRect: CGRect = self.view.frame
        aRect.size.height -= kbSize!.height

        //you may not need to scroll, see if the active field is already visible
        print("are we in here?")
        if activeField != nil {
            print("active field was not nil")
            if (CGRectContainsPoint(aRect, activeField!.frame.origin) == false) {
                let scrollPoint:CGPoint = CGPointMake(0.0, activeField!.frame.origin.y - kbSize!.height)
                scrollView.setContentOffset(scrollPoint, animated: true)
            }

        }
    }

    func keyboardOffScreen(notification: NSNotification){
        print("keyboard off screen")
        let contentInsets:UIEdgeInsets = UIEdgeInsetsZero
        scrollView.contentInset = contentInsets
        scrollView.scrollIndicatorInsets = contentInsets
        self.scrollView.setContentOffset(CGPointMake(0, -self.view.frame.origin.y/2), animated: true)
    }

My controller extends the text view delegate. Active field is declared as an optional. And it is set in the following:

    func textViewDidBeginEditing(textView: UITextView) {
        activeField = textView
        //other
    }

    func textViewDidEndEditing(commentTextView: UITextView) {
        activeField = nil
        //other
    }
user2363025
  • 5,389
  • 15
  • 41
  • 86
  • 1
    Have you seen this question? http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1 – xpereta Nov 27 '15 at 11:41
  • How is your code not working? Does it partially work? – xpereta Nov 27 '15 at 11:43
  • did you put, self.textView.delegate = self in viewDidLoad() ? – Chathuranga Silva Nov 27 '15 at 11:43
  • @ChathurangaSilva Yes i put that in my viewDidLoad – user2363025 Nov 27 '15 at 11:45
  • @xpereta it's not working at all. I'm not getting into the method keyboardOnScreen at all.. – user2363025 Nov 27 '15 at 11:46
  • @xpereta no hadn't seen that. That question is in objective-c and involves textFields as opposed to swift and textViews. I thought my initial attempt was almost right and that I was just missing something small, since it worked with textFields in another controller – user2363025 Nov 27 '15 at 11:48
  • @user2363025 If the method is not fired it means you register for keyboard notifications incorrectly somehow. So maybe you have forgotten to register your controller for a proper notification. `NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillChangeFrameNotification, object: nil)` in `viewDidLoad()` for example. – bevoy Nov 27 '15 at 12:31
  • @BiWoj I didn't have to add an observer in my other controller that involves text fields? Can I paste in yours exactly? – user2363025 Nov 27 '15 at 12:47

0 Answers0