I've used the code from How to move content of UIViewController upwards as Keypad appears using Swift trying to prevent the keyboard from covering my view.
Somehow I can't get it to work (mainly because of my n00b programming "skills")
This is the error I got: [NSObject: AnyObject] does not have a member named 'valueForKey'
on this line of code: let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;
I guess I'll have to create a IBAction of some sort, but nothing I try works. What do I have to do to get the code to work? And why do I have to do it? What does NSObject do?
here's the code:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self);
}
func keyboardWillShow(sender: NSNotification) {
let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
var frame = self.textField.frame;
frame.origin.y = frame.origin.y - rect.height;
self.textField.frame = frame;
}
func keyboardWillHide(sender: NSNotification) {
let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
var frame = self.textField.frame;
frame.origin.y = frame.origin.y + rect.height;
self.textField.frame = frame;
}