iOS 鍵盤自適應(IQKeyboardManager)使用小結


IQKeyboardManager Github地址 :https://github.com/hackiftekhar/IQKeyboardManager

經常在開發一個應用程序,我們遇到了一個問題,iPhone的鍵盤上滑覆蓋的UITextField / UITextViewIQKeyboardManager可以防止鍵盤滑動問題和覆蓋UITextField / UITextView無需你輸入任何代碼,不需要額外的設置要求。使用IQKeyboardManager你只需要添加源文件到你的項目。

主要特點

1)無代碼 
2)自動工作 
3)沒有更多的UIScrollView
4)沒有更多的子類 
5)沒有更多的手動工作 
6)沒有更多#imports

截圖


IQKeyboardManager 支持 CocoaPods

pod ‘IQKeyboardManager’

管理

UINavigationBar

如果你不使用storyboardxib創造你的視圖。你需要重寫-(void)UIViewController loadview方法,需要設置一個UIScrollView實例self.view

-(void)loadView
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = scrollView;
}

為某一個ViewController禁用IQKeyboardManager

如果你想在某個 viewcontroller 禁用 IQKeyboardManager 你應該在 ViewDidAppear 中禁用IQKeyboardManager,而在ViewWillDisappear啟用它 
代碼:

#import "IQKeyboardManager.h"@implementationExampleViewController
{
    BOOL _wasKeyboardManagerEnabled;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    _wasKeyboardManagerEnabled = [[IQKeyboardManager sharedManager] isEnabled];
    [[IQKeyboardManager sharedManager] setEnable:NO];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:_wasKeyboardManagerEnabled];
}

@end

鍵盤的回車鍵處理

1)創建一個實例變量實例化IQKeyboardReturnKeyHandler 在 ViewController 的 viewDidLoad 中

代碼:

@implementationViewController
{
    IQKeyboardReturnKeyHandler *returnKeyHandler;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
}

改變鍵盤上的返回鍵。

設置實例變量為零的dealloc方法
-(void)dealloc
{
    returnKeyHandler = nil;
}

UIToolbar(IQToolbar)

1)如果你不想添加一個特定的自動工具欄在鍵盤上方,應該添加一個類作為它的工具欄
textField.inputAccessoryView = [[UIView alloc] init];
2)如果你需要自己控制上/下/完成按鈕,那么應該使用UIView類的方法,創建你的文本框工具欄。

代碼:

-(void)viewDidLoad
{
    [super viewDidLoad];

    //Adding done button for textField1
    [textField1 addDoneOnKeyboardWithTarget:self action:@selector(doneAction:)];

    //Adding previous/next/done button for textField2
    [textField2 addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];

    //Adding cancel/done button for textField3
    [textField3 addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:)]; } /*! previousAction. */ -(void)previousAction:(id)button { //previousAction } /*! nextAction. */ -(void)nextAction:(id)button { //nextAction } /*! doneAction. */ -(void)doneAction:(UIBarButtonItem*)barButton { //doneAction } /*! cancelAction. */ -(void)cancelAction:(UIBarButtonItem*)barButton { //cancelAction }

功能和使用

UIKeyboard 處理
+(instancetype)sharedManager : Returns the default singleton instance.

@property BOOL enable : Use this to enable/disable managing distance between keyboard & textField/textView).

@property CGFloat keyboardDistanceFromTextField : Set Distance between keyboard & textField. Can't be less than zero. Defaultis10.@property BOOL preventShowingBottomBlankSpace : Prevent to show bottom blanck area when keyboard slide up the view.
IQToolbar處理
@property BOOL enableAutoToolbar : Enable autoToolbar behaviour. If It is set to NO. You have to manually create UIToolbar for keyboard. Defaultis YES.

@property IQAutoToolbarManageBehaviour toolbarManageBehaviour : Setting toolbar behaviour to IQAutoToolbarBySubviews to manage previous/next according to UITextField's hierarchy in it's SuperView. Set it to IQAutoToolbarByTag to manage previous/next according to UITextField's tag propertyin increasing order. Defaultis IQAutoToolbarBySubviews.

@property BOOL shouldToolbarUsesTextFieldTintColor : If YES, then uses textField's tintColor propertyfor IQToolbar, otherwise tintColor is black. Defaultis NO. 

@property BOOL shouldShowTextFieldPlaceholder : If YES, then it add the textField's placeholder text on IQToolbar. Defaultis YES. @property UIFont *placeholderFont : placeholder Font. Defaultis nil. Defaultis YES.
UITextView處理
@property BOOL canAdjustTextView : Giving permission to modify TextView's frame. Adjust textView's frame when it is too big in height. Defaultis NO.

@property BOOL shouldFixTextViewClip : Adjust textView's contentInset to fix fix for iOS 7.0.x -(#Stackoverflow). Defaultis YES.
UIKeyboard 外觀覆蓋
@property BOOL overrideKeyboardAppearance : Override the keyboardAppearance for all textField/textView. Defaultis NO.

@property UIKeyboardAppearance keyboardAppearance : If overrideKeyboardAppearance is YES, then all the textField keyboardAppearance issetusing this property.
關閉UITextField/UITextView
@propertyBOOL shouldResignOnTouchOutside : Resign textField if touched outside of UITextField/UITextView.

-(void)resignFirstResponder : Resigns currently first responder field.
UISound處理
@property BOOL shouldPlayInputClicks : If YES, then it plays inputClick sound onnext/previous/done click. Defaultis NO.
UIAnimation
@property BOOL shouldAdoptDefaultKeyboardAnimation : If YES, thenuses keyboard default animation curve style to move view, otherwise uses UIViewAnimationOptionCurveEaseOut animation style. Defaultis YES.

特征

1)支持設備方向。 
2)啟用/禁用鍵盤消息時,需要設置 enable 的布爾值。 
3)簡單的集成。 
4)作為一個textField/textViewAutoHandle UIToolbar需要設置 enableAutoToolbar 的布爾值。 
5)可以由父視圖AutoHandle UIToolbartextField/textView,使用toolbarManageBehaviour枚舉。 
6)方便地添加上下和完成按鈕鍵盤UIToolbar UIView類,自動使用enableAutoToolbar布爾值。 
7)啟用/禁用,下/上一個按鈕類的方法,自動使用enableAutoToolbar布爾值。 
8)鍵盤設置距離文本框使用keyboardDistanceFromTextField。 
9)鍵盤觸摸外面用shouldResignOnTouchOutside. 禁用。 
10)管理的框架時,UITextView高度太大,使用canAdjustTextView 設置適合屏幕。 
11)適用在UITableView/UIScrollView 中的UITextField/UITextView
12)可以輸入聲音在點擊“下/上一頁/完成”時。

 

以上就介紹了iOS 鍵盤自適應(IQKeyboardManager)使用小結,包括了方面的內容,希望對IOS開發有興趣的朋友有所幫助。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM