键盘处理——使用
Map |
window会发送通知,我们接收通知就可以了
1.布局:
2.拉成属性(私有):
@property
(
weak
,
nonatomic
)
IBOutlet
UIView
*inputView;
3.实现
//
程序显示的时候调用
- ( void )viewWillAppear:( BOOL )animated
{
[ super viewWillAppear :animated];
// 先拿到通知中心
NSNotificationCenter *center = [ NSNotificationCenter defaultCenter ];
// 收听通知
- ( 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 ];
[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 ];
- ( 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. 获取键盘的高度
// 生成字典
- ( 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. 设置动画
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;
// 动画是先快后慢还是先快后慢
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 ];
- ( 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 ;
{
if (! _messages ) _messages = [[ NSMutableArray alloc ] init ];
return _messages ;
}
- ( IBAction )inputCompletion:( UITextField *)sender
{
//
把输入的信息加入到
messages
中
[ self . messages addObject :sender. text ];
// 刷新数据
[ self . tableView reloadData ];
}
[ 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;
}
- ( 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;
}