Leif160519的blog Leif160519的blog

——————

目录
IOS点击键盘以外空白区域隐藏键盘的4种常见写法
/    

IOS点击键盘以外空白区域隐藏键盘的4种常见写法


###在IOS开发中,,经常要用到输入框,,但是在我们输入完之后,键盘下面的东西我们需要点击的话有时候也不好操作,必须点击键盘上的done才能将其隐藏,这样的用户体验是很不好的,为了解决这一情况,我整理了两种隐藏键盘的方法

Viewcontroller.m

首先先创建一个textfield并初始化,例如:

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UITextField * textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(40, 100, [UIScreen mainScreen].bounds.size.width-80, 40)];
    
    _textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_textField];
}

@end

效果:
这里写图片描述

注意若点击textfield键盘没有弹出的话,可以使用Command+K调出键盘,再次使用则键盘消失

方法一:

最常见的,即给最外层view添加一个手势UITapGestureRecognizer,完整代码如下:

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UITextField * textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(40, 100, [UIScreen mainScreen].bounds.size.width-80, 40)];
    
    _textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_textField];
    
    //添加手势
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTapped:)];
    [self.view addGestureRecognizer:tap];
}
//点击事件
-(void)viewTapped:(UIGestureRecognizer *)gesture
{
    [_textField resignFirstResponder];
    //或
    //[self.view endEditing:YES];
}
@end

方法二:

利用-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event函数,完整代码如下:

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UITextField * textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(40, 100, [UIScreen mainScreen].bounds.size.width-80, 40)];
    
    _textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_textField];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_textField resignFirstResponder];
}

@end

方法一和方法二的演示效果如下:
这里写图片描述

方法三:

点击键盘的done按钮(中文的换行)隐藏键盘,需要添加UItextField代理方法,完整代码如下(此方法仅适用于textfield控件):

#import "ViewController.h"

//引入代理
@interface ViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)UITextField * textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(40, 100, [UIScreen mainScreen].bounds.size.width-80, 40)];
    _textField.delegate = self;
    _textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_textField];

}
//代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_textField resignFirstResponder];
    return YES;
}
@end

演示效果如下:
这里写图片描述

方法四:

比如聊天界面(tableview)上下拖动页面使键盘消失的方法:

/** 点击拖曳聊天区的时候,缩回键盘 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    // 缩回键盘
    [self.view endEditing:YES];
}

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题IOS点击键盘以外空白区域隐藏键盘的4种常见写法
作  者Leif160519
出  处https://github.icu/articles/2019/08/23/1566541367106.html
关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!