IQKeyboardManager 問題錦集

来源:http://www.cnblogs.com/lurenq/archive/2017/09/27/7601266.html
-Advertisement-
Play Games

Keep UINavigationBar at the top (Don't scroll with keyboard) (#21, #24) If you don't want to hide the default UINavigationBar of UINavigationControlle ...


Keep UINavigationBar at the top (Don't scroll with keyboard)

(#21#24)

If you don't want to hide the default UINavigationBar of UINavigationController when keyboardManager slides up the view, then just change the UIView class to UIScrollView from the storyboard or xib. If you are using Autoresizing then you must set correct contentSize of scrollView or if you are using Autolayout then make sure scrollView is able to get it's contentSize from constraints.

image

If you are not using storyboard or xib and creating your view programmatically. Then you need to override '-(void)loadView' method of UIViewController, and need to set an UIScrollView instance to self.view.

    -(void)loadView
    {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        scrollView.contentSize = CGSizeMake(CONTENT_WIDTH, CONTENT_HEIGHT); //You may not need this code if you are working with Autolayout and scrollView is able to get it's contentSize from constraints.
        self.view = scrollView;
    }

Working with TopLayoutGuide and BottomLayoutGuide

Technically IQKeyboardManager moves upwards/downwards of currently presentedViewController's view. So if you're pinning your UITextfield/UITextView with TopLayoutGuide/BottomLayoutGuide then you're saying Keep x distance from screen top(I don't care about where is self.view)'. In this case your view is moved upwards but textField remains at same position and keeping x distance from screen top.

To fix it, just let IQKeyboardManager know the constraint which is pinned with TopLayoutGuide/BottomLayoutGuide, just map TopLayoutGuide/BottomLayoutGuide constraint with IQLayoutGuideConstraint. Here is the screenshot:- image

If your textFields are inside any UIView and your UIView is pinned with TopLayoutGuide/BotomLayoutGuide then also you can map TopLayoutGuide/BottomLayoutGuide constraint with IQLayoutGuideConstraint. here are the screenshots:- imageimage

Working with Full Screen UITextView

Often we have a situation where a full screen UITextView need to show in full screen mode with keyboard handling. To deal with this kind of situation, here is an easy workaround.

Assuming that UITextView needs to be displayed in full screen within a ViewController View and default UINavigationBar of UINavigationController is displaying at at the top of ViewController. Assuming that Adjust Sroll View Insetscheckmark is ticked. Add these constraint to UITextView:-

image

  • Top Space to SuperView
  • Leading Space to SuperView
  • Trailing Space to SuperView
  • Bottom Space to Bottom Layout Guide (Important)

Connect bottom layout guide constraint with IQLayoutGuideConstraint and that's all. You have a full working UITextViewController.

image

Working with Chat Screen UITableView

Often we have another situation where we have to implement our own Chat Style Screen with keyboard handling. To deal with this kind of situation, here is an easy workaround.

Assuming that ChatViewController is subclass of UIViewController not UITableViewController. Assuming that ChatViewController has UITableView at top and UIView at bottom. Bottom UIView contains a UITextField/UITextViewwith a Send button. Assuming that default UINavigationBar of UINavigationController is displaying at at the top of ChatViewController. Assuming that Adjust Sroll View Insets checkmark is ticked. Add these constraint to UITableView:-

image

  • Top Space to SuperView
  • Leading Space to SuperView
  • Trailing Space to SuperView
  • Bottom Space to bottom UIView

Add thse constraint to bottom UIView

  • Leading Space to SuperView
  • Trailing Space to SuperView
  • Bottom Space to Bottom Layout Guide (Important)

Connect bottom layout guide constraint with IQLayoutGuideConstraint.

Map UITextField/UITextView Outlet with a textField object in ChatViewController.

Add this two line in viewDidLoad

    self.textField.inputAccessoryView = [[UIView alloc] init];  //This will remove toolbar which have done button.
    self.textField.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.

That's all. You have a working keyboard handling with ChatViewController.

image

Enable/Disable distance handling between different ViewController's

(#117#139#516#541#572)

If you would like to ignore IQKeyboardManager.enabled property for some ViewController and would like to enable/disable IQKeyboardManager between different ViewController's then add ViewController class to disabledDistanceHandlingClassesor enabledDistanceHandlingClasses NSMutableSet property.

Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[IQKeyboardManager sharedManager].enabledDistanceHandlingClasses addObject:[EnabledViewController class]];
    [[IQKeyboardManager sharedManager].disabledDistanceHandlingClasses addObject:[DisabledViewController class]];
    return YES;
}

Swift

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        IQKeyboardManager.sharedManager().enable = true
        IQKeyboardManager.sharedManager().enabledDistanceHandlingClasses.append(EnabledViewController.self)
        IQKeyboardManager.sharedManager().disabledDistanceHandlingClasses.append(DisabledViewController.self)
        return true
    }

Enable/Disable UIToolbar between different ViewController's

(#391#530)

If you would like to ignore IQKeyboardManger.enableAutoToolbar property for some ViewController and would like to enable/disable Auto Toolbar between different ViewController's then add ViewController class to disabledToolbarClasses or enabledToolbarClasses NSMutableSet property.

Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[IQKeyboardManager sharedManager].enabledToolbarClasses addObject:[ToolbarEnabledViewController class]];
    [[IQKeyboardManager sharedManager].disabledToolbarClasses addObject:[ToolbarDisabledViewController class]];
    return YES;
}

Swift

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        IQKeyboardManager.sharedManager().enable = true
        IQKeyboardManager.sharedManager().enabledToolbarClasses.append(ToolbarEnabledViewController.self)
        IQKeyboardManager.sharedManager().disabledToolbarClasses.append(ToolbarDisabledViewController.self)
        return true
    }

Show Previous/Next arrow buttons for textField which are not direct disblings

(#154#179#380#406#503#517#524#537#540#549)

If your textFields are on different View and Previous/Next arrow are not visible to navigate between textField. Then you should put those all Views inside IQPreviousNextView like this:-

image

If you would like to use your own SpecialView (subclass of UIView) instead of default IQPreviousNextView then you can add your own SpecialView class to toolbarPreviousNextAllowedClasses NSMutableSet property.

Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[IQKeyboardManager sharedManager].toolbarPreviousNextAllowedClasses addObject:[SpecialView class]];
    return YES;
}

Swift

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        IQKeyboardManager.sharedManager().enable = true
        IQKeyboardManager.sharedManager().toolbarPreviousNextAllowedClasses.append(SpecialView.self)
        return true
    }

Hide UIToolbar for specific UITextField/UITextView

(#89#533)

If you don't want to add automatic toolbar over keyboard for specific textField then you should add a new UIView as it's toolbar like this

textField.inputAccessoryView = [[UIView alloc] init];

Change UIToolbar Done button text or replace it with some other icon

(#538#557)

If you would like to change toolbar Done button text then you can use toolbarDoneBarButtonItemText property to do the same

    [IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = @"Save";

or if you would like to replace this with an image then you should could do like this:-

    [IQKeyboardManager sharedManager].toolbarDoneBarButtonItemImage = [UIImage imageNamed:@"save"];

Full customise control over previous/next/done button for specific UITextField/UITextView

(#40)

If you need full control over the previous/next/done button then you should use the UIView category methods to add toolbar over your textField. The UIView category methods are defined in IQUIView+IQKeyboardToolbar.h file.

You may need to import IQUIView+Hierarchy category

#import "IQUIView+Hierarchy.h"

Then add custom toolbar like this.

-(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:(UIBarButtonItem*)button
{
    //previousAction
}

/*!	nextAction. */
-(void)nextAction:(UIBarButtonItem*)button
{
    //nextAction
}

/*!	doneAction. */
-(void)doneAction:(UIBarButtonItem*)barButton
{
    //doneAction
}

/*!	cancelAction. */
-(void)cancelAction:(UIBarButtonItem*)barButton
{
    //cancelAction
}

Use keyboard toolbar placeholder as action button

If you would like to use keyboard toolbar placeholder text as action buttons to do something special.

image image

This is now very easy with just 2 lines of code like this:-

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [textField4 setTitleTarget:self action:@selector(savedUsersAction:)];  //This will convert toolbar placeholder to button
    textField4.placeholderText = @"Saved Users";  //This is optional (If you would like to override default placeholder text)
}

-(void)savedUsersAction:(UIButton*)sender
{
    //Do your custom work here
    ...
}

Get notified when tapping on previous/next/done button for specific UITextField/UITextView

(#426#475#492)

If you only would like to get notified when user taps on previous/next/done button then register customised target. Note that this will not override default behaviour of prevous/next/done button but this will notify you when user taps on those buttons.

You may need to import IQUIView+Hierarchy category

#import "IQUIView+Hierarchy.h"

Then register custom selector.

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [textField setCustomPreviousTarget:self action:@selector(previousAction:)];
    [textField setCustomNextTarget:self action:@selector(nextAction:)];
    [textField setCustomDoneTarget:self action:@selector(doneAction:)];
}

/*!	previousAction. */
-(void)previousAction:(UIBarButtonItem*)button
{
    //previousAction
}

/*!	nextAction. */
-(void)nextAction:(UIBarButtonItem*)button
{
    //nextAction
}

/*!	doneAction. */
-(void)doneAction:(UIBarButtonItem*)barButton
{
    //doneAction
}

Hide Previous/Next arrow of UIToolbar

(#546#548#579)

If you don't want to show Previous/Next arrow with toolbar and only want to show *Done button only, then set shouldHidePreviousNext to NO.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [IQKeyboardManager sharedManager].shouldHidePreviousNext = YES;
    return YES;
}

Keyboard Return Key Handling

(#38#63)

If you would like to use keyboard Return Key as Next/Done button, then you can use IQKeyboardReturnKeyHandler.

Create an instance variable of IQKeyboardReturnKeyHandler and instantiate it in viewDidLoad with ViewController object like this

@implementation ViewController
{
    IQKeyboardReturnKeyHandler *returnKeyHandler;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

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

-(void)dealloc
{
    returnKeyHandler = nil;
}

It assign all the responderView delegates to self, and change keybord Return Key to Next key. If you would like to get callback of textField delegate methods then you should make returnKeyHandler.delegate = self.

原文鏈接:

https://github.com/hackiftekhar/IQKeyboardManager/blob/master/MANUAL%20MANAGEMENT.md

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、jQuery 中的常用函數 1) $.map(Array,fn); 對數組中的每個元素,都用fn進行處理,fn將處理後的結果返回,最後得到一個數組 2) $.each(Array,fn); 對數組中的每個元素,調用fn這個函數進行處理,但是,沒有返回值,比上例更常用 二、jQuery 對象和Do ...
  • 最近在前端開發中,遇到一個JavaScript 的問題。 用Chrome,Firefox,運行結果正常。但是在IE裡面確出現了"對象不支持find屬性或方法"的錯誤. 看了下官方文檔才發現他是不支持IE的。https://developer.mozilla.org/en-US/docs/Web/Ja ...
  • 轉自博客園: 現在有一個數據,需要你渲染出對應的列表出來: var data = [ {"id":1}, {"id":2}, {"id":3}, {"id":4}, ]; var str="<ul>"; data.forEach(function(v,i){ str+="<li><span>"+v. ...
  • XMLHttpRequest對象 一、XMLHttpRequest對象 1.Ajax能夠是實現非同步傳輸,所依賴的就是JavaScript中的XMLHttpRequest 2.XMLHttpRequest對象是XMLHttp組件的對象,它是一個抽象對象,允許腳本從伺服器獲取返回的eXML數據或將數據發 ...
  • Https系列會在下麵幾篇文章中分別作介紹: 一:https的簡單介紹及SSL證書的生成二:https的SSL證書在伺服器端的部署,基於tomcat,spring boot三:讓伺服器同時支持http、https,基於spring boot四:https的SSL證書在Android端基於okhttp ...
  • 本文介紹了Android下三種獲取FPS的方法,在調試及優化Android性能時可做參考。 ...
  • 轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/77678577 本文出自 "【趙彥軍的博客】" 其他 "Groovy 使用完全解析 http://blog.csdn.net/zhaoyanjun6/article/details/7 ...
  • 前言 一般iOS中的armv7、armv7s、arm64、i386、x86_64這些都代表了什麼?在Xcode中如何選擇? 介紹 armv7、armv7s、arm64都是ARM處理器的指令集。 i386、x86_64是mac處理器的指令集 這些指令集在哪些設備中有用到呢? 在實際開發中的選擇問題 X ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...