由于不敢冒然在客户端上写,我于是先写了一个demo。成功的实现了键盘出现时候,view整体的上移。
1.先看看我们demo的分析截图。
2.显而易见,view随着键盘的呈现进行了上移
那么,解决的思路是什么呢?
一,通过通知来解决问题。
简单的说,是通过 通知 来检测 键盘,如果键盘出现或者消失,调用相应的方法即可。
1)一点一点的说,我们先看看我们的vc中得 viewdidload方法
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillHideNotification object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
我们首先建立了一个消息中心,并发布了一条通知。当键盘将要出现的时候调用@SEL方法。稍后我讲讲解keyboardWillShow:方法的详细内容。
也许你已经看到了我注释的那个关于键盘隐藏的方法调用通知,在键盘消失的时候其实我没有取消我的通知的方法。这样子不好。
2)该功能最重要的方法是 keyboardWillShow 方法
- (void)setViewMoveUp:(BOOL)moveUp
{
[UIView beginAnimations:nil context:NULL];//简单的说,就是动画的设置
[UIView setAnimationDuration:0.5]; //动画的运行时间
CGRect rect = self.view.frame; //得到我们当前view的长宽
if (moveUp) {
rect.origin.y -= KOFFSET_FOR_KEYBOARD; //对我们的moveUp进行判断,如果为真,那么表示我们的键盘即将出来,我们将要对我们的view进行剪接。y坐标减少键盘的高度,我们的view的上面相应增加
rect.size.height += KOFFSET_FOR_KEYBOARD;
}
else //反向,同上
{
rect.origin.y += KOFFSET_FOR_KEYBOARD;
rect.size.height -= KOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect; //设置我们的最终的位置和宽高
[UIView commitAnimations]; //运行动画效果
}
3)我们接受键盘事件通知的方法
- (void)keyBoardWillShow:(NSNotification *)notification
{
if ([_textfield isFirstResponder] && self.view.frame.origin.y >= 0) {
[self setViewMoveUp:YES];
}
else if(![_textfield isFirstResponder] &&self.view.frame.origin.y < 0)
{
[self setViewMoveUp:NO];
}
}
很简单,根据我们的view的y值进行判断,如果y》=0.表示我们的键盘已经出现,我们的view那也将相应的上移。
反之反向操作。
ps,后面的 else if操作是没有上面用处的。我只是多写了那么多~~⊙﹏⊙b汗
4)为什么我说是多余的,因为我没有写 keyboardWillHide方法,我在键盘取消第一响应的时候直接调用了 setViewMoveUp :NO方法。
-(IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
[self setViewMoveUp:NO];
}
二,通过textfield协议来解决问题。
1)简单的说,我们就是实现textfield的一些协议方法来解决问题
思路就是使用 textfieldBeginEdit:方法调用 setViewMoveUp方法 来实现view的上移。这样子就省去了很多的代码。先看看我们的textfield的输出口连接
2)接下来就是所有代码了。很简单,我就不多解释了
- (IBAction)textFieldBeginEditing:(id)sender
{
if ([sender isEqual: self.textfield]) {
[self setViewMoveUp:YES];
}
}
- (void)setViewMoveUp:(BOOL)moveUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGRect rect = self.view.frame;
if (moveUp) {
rect.origin.y -= KOFFSET_FOR_KEYBOARD;
rect.size.height += KOFFSET_FOR_KEYBOARD;
}
else
{
rect.origin.y += KOFFSET_FOR_KEYBOARD;
rect.size.height -= KOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)keyBoardWillShow:(NSNotification *)notification
{
if ([_textfield isFirstResponder] && self.view.frame.origin.y >= 0) {
[self setViewMoveUp:YES];
}
}
-(IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
[self setViewMoveUp:NO];
}
三。官方的通知方法
直接上代码吧~
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillHideNotification object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)keyBoardWillShow:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyBoardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.view.bounds;
newTextViewFrame.size.height = keyBoardTop - self.view.bounds.origin.y;
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval annimationDUration;
[animationDurationValue getValue:&annimationDUration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:annimationDUration];
_textfield.frame = newTextViewFrame;
[UIView commitAnimations];
}
- (void)keyBoardWillHide:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
_textfield.frame = self.view.bounds;
[UIView commitAnimations];
}
这种方法,由于太混乱,不够简洁,我就没采用也没实现、。
3.方法一说完,我就不用再放置QQ里面的代码了吧。其实就是CV过去而已。