Giter Club home page Giter Club logo

hidekeyboard's Introduction

iOS键盘隐藏的几种方法


1. Overview

在开发App的过程中,经常要求用户进行输入操作,比如登陆过程中要求输入用户名和密码。但是有可能弹出的keyboard把登陆button挡住了,从而无法点击button。

2. Solutions

可以通过实现隐藏键盘来解决这类问题。隐藏键盘的方法也有好几种,比如利用键盘的return键来实现隐藏,也可以通过点击view上其它的空白处来实现隐藏,或者通过响应自定义手势来隐藏,还可以通过给软键盘增加一个toolbar,在toolbar上面添加按钮来实现隐藏键盘,当然还有其它没有提及的方法。 常用的隐藏键盘的两个方法:

  1. [view endEditing:YES];
  2. [textField resignFirstResponder];

2.1 点击View空白处隐藏键盘

方法一:重写*touchesBegan: withEvent:*方法

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if ([textField isFirstResponder] && [touch view] != textField) {
        [textField resignFirstResponder];
    }
    [super touchesBegan: touches withEvent: event];
}

方法二:

修改xib或storyb中相关view的Custom Class为UIControl(默认是UIView),在UIControl中实现点击隐藏键盘的功能

UIControl

//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController: UIViewController

- (IBAction)hideKeyboardByTouchView:(id)sender;

@end
//ViewController.m
#import "ViewController.h"

@implemetation ViewController

- (IBAction)hideKeyboardByTouchView:(id)sender {
    [self.view endEiting:YES];
}

@end

方法三:添加点击的手势,实现隐藏键盘

- (void)tapGestureRecognizerHideKeyboard {
	UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewEndEditing:)];
	[self.view addGestureRecognizer:tap];
}

- (void)viewEndEditing:(id)sender {
	[self.view endEditing:YES];
}

2.2 点击return键实现隐藏键盘

通过给textField添加响应Did End On Exit事件的方法来实现隐藏keyboard的功能

实现隐藏键盘功能的代码

- (IBAction)hideKeyboardByReturn:(id)sender {
    [sender resignFirstResponder];
}

用这中方法隐藏键盘是有问题,如果有多个textField存在的话,需要为每一个textField添加响应Did End On Exit事件的方法,有时候return键可以需要实现其它的功能。

2.3 键盘添加一个toolbar,toolbar上添加button来实现隐藏功能

首先看一下效果图

实现代码:

- (void)addToolbarOnKeyboard:(UITextField *)textField {
	UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 30)];
	
	UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
	UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKeyboard:)];
	
	[toolbar setItems:@[spaceButton, doneButton]];
	textField.inputAccessoryView = toolbar;
}

- (void)hideKeyboard:(id)sender {
	[self.view endEditing:YES];
}

3.Conclusion

以上简单的实现了隐藏键盘的方法。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.