Chris Jones
Chris Jones

Reputation: 857

Placing UITextView on top of keyboard?

When my view controller pops up, I already call the keyboard. All I want to do is place a UITextView directly above the keyboard. I could hardcode it, but am worried about different keyboard sizes. Any ideas ?

@IBOUtlet var textView: UITextView! = UITextView()

Upvotes: 1

Views: 1566

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

You can use the UIKeyboardDidShowNotification notification to know when the keyboard has come up. You can use the keyboard frame from this notification to dynamically calculate the position of this UITextField.

override func viewDidLoad() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
}

func keyBoardDidShow(notification :NSNotification)
{
   if let frameObject: AnyObject = notification.userInfo?[UIKeyboardFrameEndUserInfoKey]
   {
        let keyboardRect = frameObject.CGRectValue()
        // use keyboardRect to calculate the frame of the textfield
   }
}

For more information read https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/index.html#//apple_ref/doc/constant_group/Keyboard_Notification_User_Info_Keys

Upvotes: 1

Related Questions