haidousm
haidousm

Reputation: 555

scrollView.scrollRectToVisible() only works in the simulator?

I've got a scroll view setup to push a view upwards when the keyboard shows up (textfield active) but it seems to perfectly work in the simulator, and work every now and then in on an actual device.

func keyboardWasShown(notification: NSNotification){

    self.scrollView.isScrollEnabled = true
    var info = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)

    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets

    var aRect : CGRect = self.view.frame
    aRect.size.height -= keyboardSize!.height
    if let activeField = self.activeField {
        if (!aRect.contains(activeField.frame.origin)){
            self.scrollView.scrollRectToVisible(activeField.frame, animated: true)

        }
    }

}

Upvotes: 0

Views: 200

Answers (1)

Pedro Pinho
Pedro Pinho

Reputation: 662

The answer is no, it should work both in simulator and device.

But i would advise you instead of creating a lot of logic to understand when the view should go up or down to use this framework instead:

KeyBoard Manager

Upvotes: 1

Related Questions