10

I'm using the IQKeyboardManger library to scroll text fields when started typing using the keyboard, but I don't want to display the default toolbar from their library. Below is the code I've used.

override func viewDidLoad() {
        super.viewDidLoad()

        self.chatTextField.inputAccessoryView = [[UIView alloc] init];  //This will remove toolbar which have done button.

        self.chatTextField.keyboardDistanceFromTextField = 8; //This will modify default distance between textField and keyboard. For exact value, please manually check how far your textField from the bottom of the page. Mine was 8pt.    

    }

enter image description here

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Gijo Varghese
  • 11,264
  • 22
  • 73
  • 122

11 Answers11

39

You can set IQKeyboardManager below properties.

I assume you have enabled the IQKeyboardManager in didFinishLaunch of app delegate like this

    IQKeyboardManager.sharedManager().enable = true

shouldShowTextFieldPlaceholder to false ==> If you want to hide placeholder toolbar section

shouldHidePreviousNext to false ==> If you want to hide next and prev button and so on.

You can enable the settings in didFinishLaunch of AppDelegate like this

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    IQKeyboardManager.sharedManager().enable = true

    IQKeyboardManager.sharedManager().enableAutoToolbar = false
    IQKeyboardManager.sharedManager().shouldShowTextFieldPlaceholder = false
    IQKeyboardManager.sharedManager().shouldHidePreviousNext = false


    return true
}
Martin Mlostek
  • 2,755
  • 1
  • 28
  • 57
Wolverine
  • 4,264
  • 1
  • 27
  • 49
  • can you help me with this one too: http://stackoverflow.com/questions/40124131/keep-a-view-always-on-top-dont-scroll-with-keyboard-in-iqkeyboardmanager – Gijo Varghese Oct 19 '16 at 07:34
  • I want to hide previous/next button but [[IQKeyboardManager sharedManager] shouldHidePreviousNext] = YES; throwing me an error shouldHidePreviousNext is not available... Any help appreciated... –  May 10 '17 at 04:53
  • shouldHidePreviousNext has deprecated, in this place use previousNextDisplayMode – MAhipal Singh Aug 30 '17 at 09:27
  • 2
    to hide next previous key use:- IQKeyboardManager.sharedManager().previousNextDisplayMode = IQPreviousNextDisplayMode.alwaysHide – Manvir Singh Dec 12 '17 at 06:09
  • can I hide toolbar for just a specific textfield? – Mashhadi Jan 01 '19 at 08:05
12

You can enable or disable the toolbar in didFinishLaunchingWithOptions of AppDelegate:

IQKeyboardManager.shared.enable = true

IQKeyboardManager.shared.enableAutoToolbar = false

For more info see Properties and functions usage

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
6

Swift 3 You must use shouldResignOnTouchOutside to resign textField if touched outside of UITextField/UITextView.

Add this in your ViewController if you want it in an specific ViewController or to override all your application in the file AppDelegate.

Inside the method:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  IQKeyboardManager.sharedManager().enable = true
  IQKeyboardManager.sharedManager().enableAutoToolbar = false
  IQKeyboardManager.sharedManager().shouldShowToolbarPlaceholder = false
  IQKeyboardManager.sharedManager().shouldResignOnTouchOutside = true
}
onCompletion
  • 6,500
  • 4
  • 28
  • 37
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
  • This was flagged as a low quality post. Please add some explanatory text and clean up the formatting of the code block. – BuvinJ Oct 25 '17 at 22:11
5

This is the way to do it for an individual view controller:

class YourViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        IQKeyboardManager.shared.disabledToolbarClasses = [YourViewController.self]
    }
}

And to prevent the vc from rising when IQKeyboardManager raises it when the keyboard is present:

IQKeyboardManager.shared.disabledDistanceHandlingClasses.append(YourViewController.self)
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
4

If you want to hide for a specific controller, you can do like this :

  • import IQKeyboardManagerSwift in your the desired View Controller.
  • Add this extension :

    // MARK: - Helper
    extension <#yourViewController#> {
    
      private func keyboardManagerVisible(_ state: Bool) {
        IQKeyboardManager.shared.enableAutoToolbar = state
      }
    }
    
  • Implement this in the life cycle :

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        self.keyboardManagerVisible(false)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        self.keyboardManagerVisible(true)
    }
    
Maximelc
  • 2,384
  • 1
  • 21
  • 17
  • this works but if pushing and popping a vc you should use `viewWillAppear` to hide the keyboard instead of `viewDidAppear` – Lance Samaria Feb 25 '22 at 13:06
4

Swift 4.0 and above For hide previous Next

IQKeyboardManager.shared.previousNextDisplayMode = .alwaysHide

Swift 4.0 and above For toolbar

IQKeyboardManager.shared.enableAutoToolbar = false
Bhargav Sejpal
  • 1,352
  • 16
  • 23
1

Swift 5.1, Xcode 11

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

    IQKeyboardManager.shared.enable = true
    IQKeyboardManager.shared.enableAutoToolbar = false
    IQKeyboardManager.shared.shouldShowToolbarPlaceholder = false
    IQKeyboardManager.shared.shouldResignOnTouchOutside = true

    return true
}
Kedar Sukerkar
  • 1,410
  • 1
  • 16
  • 22
1

Swift 5, IQKeyboardManager (6.3.0)

You can call this setup function from your didFinishLaunchingWithOptions in the app delegate:

private func setupKeyboardManager() {
    IQKeyboardManager.shared().isEnabled = true
    IQKeyboardManager.shared().isEnableAutoToolbar = false
    IQKeyboardManager.shared().shouldShowToolbarPlaceholder = false
    IQKeyboardManager.shared().previousNextDisplayMode = .alwaysHide
}

Feel free to add any other customisation you need in this method, such as shouldResignOnTouchOutside or similar.

Alessandro Francucci
  • 1,528
  • 17
  • 25
1

If you want to hide the toolbar just for the specific UITextField you can do it using:

textField.keyboardToolbar.isHidden = true
Palli
  • 510
  • 6
  • 14
0

Enable IQKeyboardManager in Swift 4

Maybe try this:

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

    //Your other code here       

    // -- enable IQKeyboardManager --
    IQKeyboardManager.shared.enable = false
    
    return true
}
Community
  • 1
  • 1
0

Swift4.2

 //Add these line into didFinishLaunch
IQKeyboardManager.shared.enable = true
IQKeyboardManager.shared.shouldResignOnTouchOutside = true        
IQKeyboardManager.shared.enableAutoToolbar = false
Sukh
  • 1,278
  • 11
  • 19