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

IQKeyboardManager Github地址html

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

主要特色框架

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

截圖ide


IQKeyboardManager 支持 CocoaPods工具

pod ‘IQKeyboardManager’this

管理es5

UINavigationBar

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

 




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

爲某一個ViewController禁用IQKeyboardManager

若是你想在某個 viewcontroller 禁用 IQKeyboardManager 你應該在 ViewDidAppear 中禁用IQKeyboardManager,而在ViewWillDisappear啓用它 
代碼:code

       #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)能夠輸入聲音在點擊「下/上一頁/完成」時。

 

延伸閱讀:

      IQKeyboardManager 
      iOS自動處理鍵盤事件的第三方庫:IQKeyboardManager

 

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

本文網址連接:http://www.codes51.com/article/detail_117701.html

相關文章
相關標籤/搜索