0

I have a text view in my project and I want when user tap the text field it moves up equals to the height of the keyboard I used this code for getting height of the keyboard and It gives me the size of the keyboard

static var sizeForOffsetKeyboard = CGFloat()

    var heightKeyboard : CGFloat?
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}

func keyboardShown(notification: NSNotification) {
    if let infoKey  = notification.userInfo?[UIKeyboardFrameEndUserInfoKey],
        let rawFrame = (infoKey as AnyObject).cgRectValue {
        let keyboardFrame = view.convert(rawFrame, from: nil)
        self.heightKeyboard = keyboardFrame.size.height

        levelChatViewController.sizeForOffsetKeyboard = heightKeyboard!

        print(levelChatViewController.sizeForOffsetKeyboard)
        // Now is stored in your heightKeyboard variable
    }
}

But I didn't get the result and the textview won't move up with the height of the keyboard

so here is the codes for offset textview

func textViewDidBeginEditing(_ textView: UITextView) {


   animateViewMoving(up: true, moveValue: levelChatViewController.sizeForOffsetKeyboard)
}
Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60
  • You can use UITableViewController for automatic scrolling. Check my ans here https://stackoverflow.com/questions/45233089/move-textfield-up-when-keyboard-displays/45233564#45233564 – luckyShubhra Aug 05 '17 at 05:00
  • I used https://stackoverflow.com/questions/26070242/move-view-with-keyboard-using-swift and its ok But when textfield is editing and user change the keyboard the textfield won't move with new keyboard – Saeed Rahmatolahi Aug 05 '17 at 05:19
  • You have to keep on adding height when keyboard based on which textfiled is currently selected. You can use UITableViewController and you can achieve what you desire with zero code. – luckyShubhra Aug 05 '17 at 05:22
  • You can check ans by Celil and Fedya and Rohit in link you shared. Those ans can be helpful in your case. – luckyShubhra Aug 05 '17 at 05:26
  • I didn't used UITableViewController and because of lots of codes I can't change it can you give me another way ? – Saeed Rahmatolahi Aug 05 '17 at 05:37
  • Have you checked ans by Celil and Fedya and Rohit in link you shared above?? – luckyShubhra Aug 05 '17 at 05:38
  • I know you would have to change it no issues. You can keep UITableViewController for future when in need. – luckyShubhra Aug 05 '17 at 05:39
  • Sorry I have make a big mistake my texts are in textview not textfield I will Edit question – Saeed Rahmatolahi Aug 05 '17 at 05:59

1 Answers1

0

Easiest way is to use IQKeyboardManagerSwift

Add pod

      pod 'IQKeyboardManagerSwift'

In your AppDelegate

     import IQKeyboardManagerSwift

inside didFinishLaunchingWithOptions

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    IQKeyboardManager.sharedManager().enable = true

     }

This will manage all the text filed of your project and you do not have to write anything anywhere else

Just try it once you will love it

To know more https://github.com/hackiftekhar/IQKeyboardManager

Subhojit Mandal
  • 450
  • 4
  • 13