Re: [问题] keyboard挡住firstresponder

楼主: Starwindd (原PO大叔)   2014-07-18 00:01:19
※ 引述《ahchie (啵B劝-福.加油)》之铭言:
: 用NSNotificationCenter接UIKeyboardWillShowNotification
: 在handler-selector function里可以知道键盘大小(高度)
: 然后再去把整个view的frame做调整:
: 编辑完了之后 如果是UITextField的话 就用UITextFieldDelegate function
: 以上~ ;)
抱歉回一下一年前的旧文。最近刚开始学iPhone Programming,也在为
这点困扰。
在UITextField输入的时候,出现的事件依序是
1. UITextFieldDelegate 的 textField***BeginEditing
2. NSNotification keyboard***Appear
3. NSNotification keyboard***Disappear
4. UITextFieldDelegate 的 textField***EndEditing
其中 2 跟 3 是在键盘浮出来的时候会出现的,也只有这两个地方可以
得到键盘尺寸的资料。
如果今天只有一个 UITextField 要移动,问题不大,只要在 2 的地方
把整个UIView拉上去,3或4拉回来就好了。可是如果有超过一个UITextField
,在UITextField之间切换时 2 跟 3 是不会被呼叫的。所以要处理UIView
移动就变成一定要在 1 。
最后我的作法是设定两个local参数,一个是 BOOL keyboardUp,另一个
是 CGFloat keyboardHeight
在 textFieldDidBeginEditing 里头,如果 keyboardUp = NO ,则什么
事情都不做,等待 keyboardWillAppear 的时候去处理UIView的移动,
并且记录keyboardHeight。反之则直接处理UIView移动。
不过这样还是觉得有点囉嗦。有比较好的方法吗?
下面是部分我的程式内容:
UITextField *activeTextField;
CGFloat lastOffset;
BOOL keyboardUp;
CGFloat keyboardHeight;
-(void)keyboardWillAppear: (NSNotification *)notification {
keyboardUp = YES;
if (activeTextField) {
// Get bottom cordination
CGFloat bottom = activeTextField.frame.origin.y +
activeTextField.frame.size.height;
// Get Screen Size
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
// Get Keyboard Size
NSValue *value = [[notification userInfo]
valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGFloat offsetKeyboard = [value CGRectValue].size.height;
keyboardHeight = offsetKeyboard;
if (bottom > screenHeight - offsetKeyboard) {
// Need to move view up
if (self.view.frame.origin.y >= 0)
{
lastOffset = bottom - (screenHeight - offsetKeyboard) + 20;
[self moveViewUp:YES offsetValue:lastOffset];
}
} else {
lastOffset = 0;
}
}
}
-(void)keyboardWillDisappear: (NSNotification *)notification {
keyboardUp = NO;
if (self.view.frame.origin.y < 0)
{
[self moveViewUp:NO offsetValue:lastOffset];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
activeTextField = textField;
if (keyboardUp) {
//recalculate offset
CGFloat bottom = activeTextField.frame.origin.y +
activeTextField.frame.size.height;
// Get Screen Size
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
if (bottom > screenHeight - keyboardHeight) {
//Need to move view
lastOffset = bottom - (screenHeight - keyboardHeight) + 20;
[self moveViewUp:YES offsetValue:lastOffset +
self.view.frame.origin.y];
} else {
[self moveViewUp:NO offsetValue:lastOffset];
}
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
activeTextField = nil;
}
-(void)moveViewUp:(BOOL)bMovedUp offsetValue:(CGFloat)offset
{
if (offset != 0) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4]; // to slide the view up
CGRect rect = self.view.frame;
if (bMovedUp) {
rect.origin.y -= offset;
rect.size.height += offset;
} else {
// revert to normal state of the view.
rect.origin.y += offset;
rect.size.height -= offset;
lastOffset = 0;
} self.view.frame = rect;
[UIView commitAnimations];
}
}
作者: tentenlee (天天)   2014-07-18 00:42:00
切换输入法时的后键盘高低的变化勒?
作者: howdiun (Howdiun)   2014-07-18 10:11:00
keyboardUp = NO 不是很需要每次textFieldDelegate都调整画面即可
作者: liddle (Guderian)   2014-07-18 11:53:00
小心iOS8 的输入法加上选字的高低

Links booklink

Contact Us: admin [ a t ] ucptt.com