想在鍵盤上添加一個按鈕,實時根據鍵盤不同高度變換按鈕位置,再不做輸入的時候點擊按鈕能夠隱藏鍵盤,這種方式在很多軟件上都有體現,然後在網上查閱了關於檢測鍵盤高度一些相關知識,以下是一個Demo,代碼有很多需要優化地方,僅供需要者參考;
先看效果:
首先是我們在ViewDidLoada()中注冊了兩個通知,[NSNotificationCenterdefaultCenter],檢測鍵盤動態,一個是鍵盤將要彈出的時候,另一個是鍵盤將要退出時候鍵盤的信息
- (void)viewDidLoad
{
NSLog(@"%@",NSStringFromSelector(_cmd));
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
檢測鍵盤消息一個六種,根據字面意思差不多都能說明函數作用
UIKeyboardWillShowNotification 通知將要發布時候顯示鍵盤
UIKeyboardDidShowNotification 通知發布後立即顯示鍵盤
UIKeyboardWillHideNotification 通知發布前撤銷鍵盤
UIKeyboardDidHideNotification 通知發布後撤銷鍵盤
UIKeyboardWillChangeFrameNotification 通知發布前迅速變化的框架的鍵盤。
UIKeyboardDidChangeFrameNotification 通知發布後立即改變在鍵盤的框架。
NSLog(@"%@",NSStringFromSelector(_cmd));是我特意加上去的,它能在控制台顯示打印出當前程序所調用的函數,我在下面每個函數都加了這一句,當我進行不同操作的時候,打印出被調用函數名,在調試程序時候比較適用吧;
注冊消息通知後,實現通知所響應的方法
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
NSLog(@"%@",NSStringFromSelector(_cmd));
NSDictionary *info = [notification userInfo];
CGRect keyboardFrame;
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
CGFloat distanceToMove = kbSize.height;
NSLog(@"---->動態鍵盤高度:%f",distanceToMove);
if (exitButton == nil) {
exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-40, self.view.frame.size.height - distanceToMove, 40.0f, 30.0f);
exitButton.frame = exitBtFrame;
[exitButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateNormal];
[self.view addSubview:exitButton];
}
exitButton.hidden=NO;
[self adjustPanelsWithKeyBordHeight:distanceToMove];
[exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown];
}
在這個函數方法中值得探討的是關於鍵盤所包含信息,因為每一次鍵盤彈出的時候也是動畫形式彈出,他的坐標位置大小包含在userInfo的字典中,現在我用
NSLog(@"-->info:%@",info);打印出info對象,這些信息都可以在不同存儲類型,取值的時候注意取值方式,此處只是提一提,希望以後有時間在做探討,
在這一段代碼上,後面注釋了5行,因為打算當鍵盤推出的時候,按鈕從視圖上移除,或者釋放按鈕,但是都導致了應用程序崩潰,後來就沒有釋放和移除操作了
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
NSLog(@"%@",NSStringFromSelector(_cmd));
if (exitButton.hidden==NO) {
exitButton.hidden = YES;
}
// if (exitButton.superview)
// {
// [exitButton removeFromSuperview];
// [exitButton release];
// }
}
-(void)adjustPanelsWithKeyBordHeight:(float) height
{
NSLog(@"%@",NSStringFromSelector(_cmd));
if (exitButton) {
CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 40, self.view.frame.size.height - height-30, 40.0f, 30.0f);
exitButton.frame = exitBtFrame;
[self.view addSubview:exitButton];
}
// UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
// if (exitButton.superview == nil)
// {
// [tempWindow addSubview:exitButton];
// // 注意這裡直接加到window上
// }
}
-(void)CancelBackKeyboard:(id)sender
{
NSLog(@"%@",NSStringFromSelector(_cmd));
[textField resignFirstResponder];
}
- (void)viewDidUnload
{
[self setTextField:nil];
exitButton=nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)dealloc {
[textField release];
[exitButton release];
[[NSNotificationCenter defaultCenter] removeObserver:self];//移除所注冊的通知
[super dealloc];
}