键盘处理

本文介绍如何在iOS应用中处理键盘的显示与隐藏,通过监听键盘通知来调整输入框位置,并结合TableView实现完整的聊天界面效果。

键盘处理——使用
Map

window会发送通知,我们接收通知就可以了
1.布局:
2.拉成属性(私有):
@property ( weak , nonatomic ) IBOutlet UIView *inputView;
3.实现
// 程序显示的时候调用
- (
void )viewWillAppear:( BOOL )animated
{
    [
super viewWillAppear :animated];
   
// 先拿到通知中心
   
NSNotificationCenter *center = [ NSNotificationCenter defaultCenter ];
   
// 收听通知
    //UIKeyboardWillShowNotification键盘将要显示的一个通知

    //UIKeyboardWillShowNotification;<-键盘将要显示
    //UIKeyboardDidShowNotification;<-键盘已经显示
    //UIKeyboardWillHideNotification;<-键盘将要隐藏
    //UIKeyboardDidHideNotification;<-键盘已经隐藏

    // 监听键盘显示
    [center
addObserver : self selector : @selector (keyboardAppear:) name : UIKeyboardWillShowNotification object : nil ];
   
// 监听键盘隐藏
    [center
addObserver : self selector : @selector (keyboardDisappear:) name : UIKeyboardWillHideNotification object : nil ];
}

// 释放后调用
- (
void )viewWillDisappear:( BOOL )animated
{
    [
super viewWillDisappear :animated];
   
// 删除
    [[
NSNotificationCenter defaultCenter ] removeObserver : self name : UIKeyboardWillShowNotification object : nil ];
    [[
NSNotificationCenter defaultCenter ] removeObserver : self name : UIKeyboardWillHideNotification object : nil ];
}

//UIKeyboardFrameBeginUserInfoKey<-键盘开始(弹起)的时候有多大        
//UIKeyboardFrameEndUserInfoKey<- 键盘结束 (隐藏) 的时候有多大          
//UIKeyboardAnimationDurationUserInfoKey<-键盘弹起的时候动画有多长

// 键盘显示 ( 弹起 ) 的方法
- (
void )keyboardAppear:( NSNotification *)notification
{
   
//1. 获取键盘的高度
   
// 生成字典
    NSDictionary *userInfo = notification.userInfo;
    NSLog(@"%@",userInfo);//此处输出下面有截图
    // 拿到动画结束之后的 frame CGRect 不能放到 NSDictionary 里,所以是 NSValue
   
NSValue *value = userInfo[ UIKeyboardFrameEndUserInfoKey ];
   
// 拿到 frame
   
CGRect keyboardFrame = [value CGRectValue ];
   
   
//2. 计算输入框的 frame
   
CGRect inputFrame = self . inputView . frame ;
   
// 设置键盘弹起后 inputView 的位置
   
// 视图的高 - 键盘的高 -inputView 的高
    inputFrame.
origin . y = self . view . bounds . size . height - keyboardFrame. size . height - inputFrame. size . height ;
   
//3. 设置动画
    //动画的时间[userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue]<-键盘动画时间[转成doubleValue] 
    NSTimeInterval duration = [userInfo[ UIKeyboardAnimationDurationUserInfoKey ] doubleValue ];
   
// 动画是先快后慢还是先快后慢
   
UIViewAnimationOptions options = [userInfo[ UIKeyboardAnimationCurveUserInfoKey ] intValue ];
    [
UIView animateWithDuration :duration delay : 0   options :options animations :^{
       
self . inputView . frame = inputFrame;
    } completion:nil];
}

// 键盘隐藏的方法
- (
void )keyboardDisappear:( NSNotification *)notification
{
   
// 拿到 frame
   
CGRect frame = self . inputView . frame ;
   
// 修改 y
    frame.
origin . y = self . view . bounds . size . height - frame. size . height ;
   
NSDictionary *userInfo = notification. userInfo ;
   
NSTimeInterval duration = [userInfo[ UIKeyboardAnimationDurationUserInfoKey ] doubleValue ];
   
UIViewAnimationOptions options = [userInfo[ UIKeyboardAnimationCurveUserInfoKey ] intValue ];
    [
UIView animateWithDuration :duration delay : 0   options :options animations :^{
       
self . inputView . frame = frame;
    }
completion : nil ];
}

//拉拽
- ( IBAction )inputCompletion:( UITextField *)sender
{
   
// 释放
    [sender resignFirstResponder];
}
此时,键盘就能实现弹起和隐藏了并且inputView会随着键盘的弹起而变化

键盘缩回去:



在上面的代码基础上修改
声明属性(私有):
@property ( strong , nonatomic ) NSMutableArray *messages;
@property ( strong , nonatomic ) UITableView *tableView;
实现:
- ( NSMutableArray *)messages
{
   
if (! _messages ) _messages = [[ NSMutableArray alloc ] init ];
   
return _messages ;
}

- (
IBAction )inputCompletion:( UITextField *)sender
{
    // 把输入的信息加入到 messages
    [
self . messages addObject :sender. text ];
   
// 刷新数据
    [
self . tableView reloadData ];
}

// 纯代码创建一个 tableView 别忘了遵守协议 < UITableViewDataSource >
static NSString *cellIdentifier = @"MessageCell" ;
- (
void )viewDidLoad
{
    [
super viewDidLoad ];
   
UITableView *tableView = [[ UITableView alloc ] initWithFrame : self . view . frame style : UITableViewStylePlain ];
   
self . tableView = tableView;
    tableView.
dataSource = self ;
   
// 注册 cell
    [tableView
registerClass :[ UITableViewCell class ] forCellReuseIdentifier : cellIdentifier ];
   
//[self.view addSubview:tableView];
   
// 因为手动创建 tableView storyboard 之后创建的, storyboard 会被 tableView
    [
self . view insertSubview :tableView atIndex : 0 ];
}

- (
NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section
{
   
return self . messages . count ;
}

- (
UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
   
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier : cellIdentifier forIndexPath :indexPath];
    cell.
textLabel . text = self . messages [indexPath. row ];
   
return cell;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值