Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide Toolbar in IQKeyboardManager iOS Swift 3

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

like image 817
Gijo Varghese Avatar asked Oct 19 '16 07:10

Gijo Varghese


People also ask

How do I dismiss Iqkeyboard in Swift?

Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing .


8 Answers

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
}
like image 125
Wolverine Avatar answered Oct 05 '22 09:10

Wolverine


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

like image 28
Bhavin Ramani Avatar answered Oct 05 '22 09:10

Bhavin Ramani


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
}
like image 29
Benjamin RD Avatar answered Oct 05 '22 09:10

Benjamin RD


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)
like image 26
Lance Samaria Avatar answered Oct 05 '22 10:10

Lance Samaria


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)
    }
    
like image 22
Maximelc Avatar answered Oct 05 '22 10:10

Maximelc


Swift 4.0 and above For hide previous Next

IQKeyboardManager.shared.previousNextDisplayMode = .alwaysHide

Swift 4.0 and above For toolbar

IQKeyboardManager.shared.enableAutoToolbar = false
like image 42
Bhargav Sejpal Avatar answered Oct 05 '22 10:10

Bhargav Sejpal


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.

like image 31
Alessandro Francucci Avatar answered Oct 05 '22 08:10

Alessandro Francucci


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
}
like image 24
Kedar Sukerkar Avatar answered Oct 05 '22 10:10

Kedar Sukerkar