美文网首页征服iOS
IQKeyboardManager

IQKeyboardManager

作者: wpf_register | 来源:发表于2016-08-06 15:19 被阅读342次

    IQKeyboardManager也许算得上最简单的处理键盘弹起遮挡UITextField/UITextView的第三方库,默认支持UITextField, UITextView, UIWebView, UIScrollView, UITableView, UICollectionView。

    基本使用

    1.cocoaPods导入IQKeyboardManager后,默认所有页面都不需要写任何代码,当键盘弹出时,view会自动上移。如果某个页面要禁用,代码如下。

    #import <IQKeyboardManager.h>
    - (void)viewWillAppear:(BOOL)animated{
       [super viewWillAppear:animated]; [IQKeyboardManager sharedManager].enable = NO;
    }
    - (void)viewWillDisappear:(BOOL)animated{
      [super viewWillDisappear:animated]; [IQKeyboardManager   sharedManager].enable = YES;
     }
    

    或者在AppDelegate.m中

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
        [[IQKeyboardManager sharedManager] disableInViewControllerClass:[ViewController class]]; return YES;
    }
    

    2.键盘的工具条上: 前后箭头+完成按钮

    //默认是YES
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    

    3.点击背景键盘回收

    - (void)viewDidLoad{
      [super viewDidLoad];
      //默认是NO
      IQKeyboardManager sharedManager] shouldResignOnTouchOutside = YES;
    }
    

    4.最最重要的一点,如果有UINavigationBar, 在view上移时,navigationBar也会跟着上移,为避名上移需要重写UIViewController的loadView方法

    //如果是xib或SB创建的view的话,直接在侧栏直接更改类为UIScrollView
    - (void)loadView{
      UIScrollView *scrollView =[ [UIScrollView alloc] initWithFrame:[[UIScreen mainScreen].bounds]];
      self.view = scrollView;
    }
    

    进阶使用

    #import <IQKeyboardManager.h>
    #import <IQUIView+IQKeyboardToolbar.h>
    
     // 键盘的工具条上: 前后箭头+完成按钮
       manager.enableAutoToolbar = YES;
     //点击工具条上的按钮的回调方法
     //[self.myTf addDoneOnKeyboardWithTarget:self action:@selector(goAction) titleText:@"工具条上固定显示的标题"];
       [self.myTf addDoneOnKeyboardWithTarget:self action:@selector(goAction) shouldShowPlaceholder:YES];
    

    PS:

    如果需要禁用第三方键盘,系统提供了方法

    - (BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier{
        return NO;
    }
    

    相关文章

      网友评论

        本文标题:IQKeyboardManager

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