应用场景:输入框(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;
}
其它特性和使用功能参考这里
结语
欢迎在评论区交流意见,如果对您有所帮助记得给个爱心哈😄~