I am trying to make my code more organised and reusable. I have some functions and notification that allows the scroll view to be moved up when keyboard shows up and scroll down when keyboard hides. It is all functioning. However, I would imaging these function will be used in multiple parts of my project that has scrollView inside UIViewcontroller. so I want to create a more resuable code rather than writing the same codes in multiple view controllers.
Currently, inside one of my view controller, I have
var keyboard = CGRect()
override func viewDidLoad() {
// Check notifications of keyboard activity
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PostVC.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PostVC.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
// Tap to hide keyboard
let hideTap = UITapGestureRecognizer(target: self, action: #selector(PostVC.hideKeyboard))
hideTap.numberOfTapsRequired = 1
self.view.userInteractionEnabled = true
self.view.addGestureRecognizer(hideTap)
}
func hideKeyboard() {
self.view.endEditing(true)
}
func keyboardWillShow(notification: NSNotification) {
keyboard = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey]!.CGRectValue())!
UIView.animateWithDuration(0.4) {
self.scrollView.contentSize.height = self.view.frame.size.height + self.keyboard.height / 2 + UITabBarController().tabBar.frame.size.height
}
}
func keyboardWillHide(notification: NSNotification) {
UIView.animateWithDuration(0.4) {
self.scrollView.contentSize.height = 0
}
}
I am abit new to trying to make code more re-usable. I am not sure if I need to create a new class or just create a extension of UIViewcontroller and put it in there. I have tried creating an extension of UIViewcontroller and do something like
func keyboardWillShow(notification: NSNotification, _scrollView: UIScrollView) { }
and pass an instance of the scrollview (@IBOutlet weak var scrollView: UIScrollView!) into the function. However, I then run into trouble with doing #selector(keyboardWillShow(_:, keyboard: keyboard, scrollView: scrollView)
. It gives me an error saying expected expression in list of expressions (I think it is complaining about _:). I might be on a totally wrong path of doing this. Can anyone please help.
Thanks,