※ 引述《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];
}
}