3

I need to adjust the frame of the presented view of a custom UIPresentationController using an animation when the keyboard becomes visible.

Animations doesn't seem to work due to automatically added constraints. I tried to create an animation by modifying these constraints, but failed.

I have been trying to fix this problem the last 8 hours but failed. Also couldn't find any useful information in the UIPresentationController documentation. Could anyone give me some advice how to solve this problem?

Im trying to reproduce an effect similar the modalPresentationStyle .Popover. Simply by reducing the frame height and origin so the keyboard won't cover any content.

I want the frame to adapt when the keyboard appears.

example PresentedVC:

import UIKit
class CustomSaleViewController: UIViewController {

private let animationDuration: Double = 0.5

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    modalPresentationStyle = UIModalPresentationStyle.Custom
}


private var originalFrameBeforeKeyboardAppread: CGRect?
func keyboardWillShow(notification: NSNotification) {

    if originalFrameBeforeKeyboardAppread == nil { originalFrameBeforeKeyboardAppread = view.frame }

    let keyboardHeight = notification.userInfo![UIKeyboardFrameEndUserInfoKey]!.CGRectValue.height
    let remainingVisibleHeight = Utils.window()!.frame.height - keyboardHeight
    let preferedSpacing: CGFloat = 20
    let maxHeight = remainingVisibleHeight - preferedSpacing * 2

    if remainingVisibleHeight > view.frame.height - preferedSpacing {
        UIView.animateWithDuration(animationDuration, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut , animations: {
            self.view.frame.origin.y = preferedSpacing
            self.view.frame.size.height = maxHeight
            }, completion: nil)
    }
}

func keyboardWillHide(notification: NSNotification) {
    if originalFrameBeforeKeyboardAppread != nil {
        UIView.animateWithDuration(animationDuration, animations: {
            self.view.frame = self.originalFrameBeforeKeyboardAppread!
            }, completion: nil)
    }
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

}
Peter Combee
  • 595
  • 4
  • 10
  • 1
    Please do not post all the code you have but only what seems relevant for answering the question. – LinusGeffarth Nov 23 '15 at 11:56
  • It's unclear what you are trying to do. Is the controller already showing when you want the animation? Is it an animation related to the keyboard? – Mika Nov 23 '15 at 13:06
  • I added a little more information to my question. Hopefully this is enough – Peter Combee Nov 26 '15 at 09:30

0 Answers0