Reputation: 121
I have a text view on the bottom of one of my views, and it would get hidden by they keyboard when it pops up if I didn't do anything. What I did was that when the keyboard is about to appear, the text view moves up 160 points. The problem is that it doesn't sit in the exact same place on every iPhone because of the different sizes. I would like to know if it was possible for the text view to sit say, 20 points on top of they keyboard, instead of pushing it 160 points up from its' origin.
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);
}
var isShown: Bool = false
var willHide: Bool = false
func keyboardWillShow(sender: NSNotification) {
isShown = true
}
func keyboardWillHide(sender: NSNotification) {
willHide = true
}
func textViewDidBeginEditing(textView: UITextView) {
if isShown == true {
self.view.frame.origin.y -= 160
}
}
func textViewDidEndEditing(textView: UITextView) {
if willHide == true {
self.view.frame.origin.y += 160
}
}
Upvotes: 0
Views: 229
Reputation: 7549
You can get the size of the keyboard from the notification object.
Something like this
NSDictionary* info = [sender userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
You can read more about this here in Apple Docs
Upvotes: 1