如何在 IQKeyboardManager 中隐藏工具栏 iOS Swift 3

How to hide Toolbar in IQKeyboardManager iOS Swift 3

我正在使用 IQKeyboardManger 库在开始使用键盘输入时滚动文本字段,但我不想显示其库中的默认工具栏。下面是我用过的代码。

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.    

    }

您可以在属性下方设置 IQKeyboardManager。

我假设你已经像这样在 app delegate 的 didFinishLaunch 中启用了 IQKeyboardManager

    IQKeyboardManager.sharedManager().enable = true

shouldShowTextFieldPlaceholderfalse ==> 如果你想隐藏占位符工具栏部分

shouldHidePreviousNext to false ==> 如果你想隐藏下一个和上一个按钮等等。

您可以像这样在 AppDelegatedidFinishLaunch 中启用设置

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
}

您可以在 AppDelegatedidFinishLaunchingWithOptions 中启用或禁用工具栏:

IQKeyboardManager.shared.enable = true

IQKeyboardManager.shared.enableAutoToolbar = false

有关详细信息,请参阅 Properties and functions usage

Swift 3 如果在 UITextField/UITextView 之外触摸,您必须使用 shouldResignOnTouchOutside 退出 textField。

如果您希望在特定的 ViewController 中使用它,或者覆盖文件 AppDelegate.

中的所有应用程序,请将其添加到您的 ViewController

方法内部:

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
}

如果你想隐藏一个特定的控制器,你可以这样做:

  • import IQKeyboardManagerSwift 在你想要的视图控制器中。
  • 添加此扩展程序:

    // MARK: - Helper
    extension <#yourViewController#> {
    
      private func keyboardManagerVisible(_ state: Bool) {
        IQKeyboardManager.shared.enableAutoToolbar = state
      }
    }
    
  • 在生命周期中实现这个:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        self.keyboardManagerVisible(false)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        self.keyboardManagerVisible(true)
    }
    

在 Swift 4

中启用 IQKeyboardManager

也许试试这个:

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

    //Your other code here       

    // -- enable IQKeyboardManager --
    IQKeyboardManager.shared.enable = false
    
    return true
}

Swift5.1,Xcode11

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
}

Swift 4.0及以上 For hide previous Next

IQKeyboardManager.shared.previousNextDisplayMode = .alwaysHide

Swift 4.0 及以上 对于工具栏

IQKeyboardManager.shared.enableAutoToolbar = false

Swift 5、IQKeyboardManager(6.3.0)

您可以在应用程序委托中从 didFinishLaunchingWithOptions 调用此设置函数:

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

您可以在此方法中随意添加您需要的任何其他自定义,例如 shouldResignOnTouchOutside 或类似内容。

Swift4.2

 //Add these line into didFinishLaunch
IQKeyboardManager.shared.enable = true
IQKeyboardManager.shared.shouldResignOnTouchOutside = true        
IQKeyboardManager.shared.enableAutoToolbar = false

这是为单个视图控制器执行此操作的方法:

class YourViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

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

当 IQKeyboardManager 在键盘存在时引发它时prevent the vc from rising

IQKeyboardManager.shared.disabledDistanceHandlingClasses.append(YourViewController.self)