浅谈IQKeyboardManager

作者: DeadRabbit | 来源:发表于2017-05-17 16:00 被阅读1430次

    应用场景:输入框(UITextField/UITextView)被弹起的键盘覆盖,导致用户就看不到输入结果,体验较差。
    ** IQKeyboardManager:** IQKeyboardManager可以很容易地解决弹起键盘遮盖输入框的问题,并且易于集成。注:IQKeyboardManager已经提供Swift版本。😊
    下载:官方下载地址

    官方效果

    安装

    ** Installation with Cocoapod:-**
    注意:-iOS 7 能支持的最高版本是3.3.7
    IQKeyboardManager (Objective-C):- IQKeyboardManager is available through CocoaPods, to install it simply add the following line to your Podfile:

    iOS8 and later pod 'IQKeyboardManager'
    iOS7 pod 'IQKeyboardManagerSwift', '3.3.7'

    IQKeyboardManager (Swift):- IQKeyboardManagerSwift is available through CocoaPods, to install it simply add the following line to your Podfile: (#236)

    Swift 3.1 (Xcode 8.0)
    pod 'IQKeyboardManagerSwift'
    Or
    pod 'IQKeyboardManagerSwift', '4.0.9'
    Swift 3.0(3.0.2) (Xcode 8.2) pod 'IQKeyboardManagerSwift', '4.0.8'
    Swift 2.2 or 2.3 (Xcode 7.3) pod 'IQKeyboardManagerSwift', '4.0.5'
    Swift 2.1.1 (Xcode 7.2) pod 'IQKeyboardManagerSwift', '4.0.0'
    Swift 2.0 (Xcode 7.0) pod 'IQKeyboardManagerSwift', '3.3.3.1'

    In AppDelegate.swift, just import IQKeyboardManagerSwift framework and enable IQKeyboardManager.

    import IQKeyboardManagerSwift
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
          IQKeyboardManager.sharedManager().enable = true
          return true
        }
    }
    

    Installation with Carthage

    Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

    You can install Carthage with Homebrew using the following command:

    $ brew update
    $ brew install carthage

    To integrate IQKeyboardManger or IQKeyboardManagerSwift into your Xcode project using Carthage, specify it in your Cartfile:

    github "hackiftekhar/IQKeyboardManager"
    Run carthage to build the frameworks and drag the appropriate framework (IQKeyboardManager.framework or IQKeyboardManagerSwift.framework) into your Xcode project according to your need. Make sure to add only one framework and not both.

    Installation with Source Code:-

    Github tag

    IQKeyboardManager (Objective-C):- Just drag and drop IQKeyboardManager directory from demo project to your project. That's it.

    IQKeyboardManager (Swift):- Drag and drop IQKeyboardManagerSwift directory from demo project to your project

    In AppDelegate.swift, just enable IQKeyboardManager.

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
          IQKeyboardManager.sharedManager().enable = true
    
          return true
        }
    }
    

    使用

    控制自动键盘处理事件 整个项目是否可用

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        //关闭设置为NO, 默认值为NO.
        [IQKeyboardManager sharedManager].enable = YES;
    }
    

    点击键盘以外的地方收起键盘
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    内联编辑(Inline Editing), 这就需要隐藏键盘上的工具条(默认打开)
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    不需要键盘上的工具条
    textField.inputAccessoryView = [[UIView alloc] init];
    在某个页面禁止自动键盘处理事件

    - (void) viewWillAppear: (BOOL)animated {
             //打开键盘事件相应
              [IQKeyboardManager sharedManager].enable = YES;
    }
    
    - (void) viewWillDisappear: (BOOL)animated {
             //关闭键盘事件相应
              [IQKeyboardManager sharedManager].enable = NO;
    }
    

    其它特性和使用功能参考这里

    结语

    欢迎在评论区交流意见,如果对您有所帮助记得给个爱心哈😄~

    相关文章

      网友评论

      • 不辣先生:我导入这个框架根本用不了, IQKeyboardManager.sharedManager().enable = true
        这句直接报错,何解?
      • 让我走的潇洒一点:也不支持添加在UIwindow上的输入框吗
      • 西叶lv:iOS11,Xcode9,键盘工具条没有上下切换按钮和Done按钮,只有占位提示内容,踩到坑了么?
        西叶lv:还没,这两天十九大,梯子没了
        yuqian__zhang:我也遇到这个问题 ,请问怎么解决的 ?
      • 半生飘零:为什么其他正常,但是在cell里面的textfield becomeFirstResponder输入框还是会被挡住
        DeadRabbit:@半生飘零 厉害 还没仔细看源码逻辑 ~ 延迟到拿到坐标之后的生命周期里 是个好的解决方法~
        半生飘零:@DeadRabbit 好像是线程问题 即tableview还没刷出来 就走iqkey里面的方法 坐标拿不到 ,所以我延迟执行becomefirst 就ok了
        DeadRabbit:他们不推荐 放在scrollview或是 其子类里面 可能会不奏效 ~

      本文标题:浅谈IQKeyboardManager

      本文链接:https://www.haomeiwen.com/subject/ggedxxtx.html