Đoạn mã sau (xin lỗi cho độ dài) hiển thị một hành vi lạ dưới iOS 4.3 (có thể là phiên bản khác). Trong ví dụ này, có ba UITextField
s có ba bàn phím có kích thước khác nhau. Nếu bạn bắt đầu chỉnh sửa một trường văn bản và sau đó chạm vào "quay lại" loại bỏ bàn phím, mỗi lần kích thước bàn phím được trả lại chính xác trong UIKeyboardWillShowNotification
và UIKeyboardDidShowNotification
sử dụng UIKeyboardFrameBeginUserInfoKey
.UIKeyboardWillShowNotification & UIKeyboardDidShowBáo cáo báo cáo độ cao bàn phím sai
xem dưới đây:
- (void) keyboardWillShowNotification:(NSNotification *)aNotification
và
- (void) keyboardDidShowNotification:(NSNotification *)aNotification
Lưu ý rằng đây là hành vi dự kiến.
action reported keyboard size expected keyboard size
--------------------- ---------------------- ----------------------
touch one & return 100 100
touch two & return 200 200
touch normal & return 216 216
n & return keyboard size(n) keyboard size(n)
Hành vi không mong muốn là nếu bạn bắt đầu chỉnh sửa trường văn bản có kích thước bàn phím đầu tiên được báo cáo (dự kiến). Khi bạn chạm vào trường văn bản thứ hai (mà không cần chạm trở lại), kích thước của bàn phím đầu tiên được báo cáo (không mong muốn) thay vì kích thước của phần thứ hai. Khi bạn chạm vào trường văn bản thứ ba (mà không cần chạm trở lại), kích thước của kích thước bàn phím thứ hai được báo cáo (không mong muốn) thay vì kích thước của phần thứ ba. Từ lần thứ hai đến lần thứ n, có vẻ như nó đang báo cáo kích thước bàn phím trước đó không phải là kích thước sẽ được hiển thị.
action reported keyboard size expected keyboard size
------------ ---------------------- ----------------------
touch one 100 100
touch two 100 200
touch normal 200 216
touch one 216 100
n keyboard size(n-1) keyboard size(n)
Trước khi gửi báo cáo lỗi, tôi chỉ muốn đảm bảo rằng tôi đã không xem xét bất kỳ thứ gì.
FYI Tôi stubbled khi điều này trong khi cố gắng để làm điều đúng (sử dụng UIKeyboardWillShowNotification
hoặc UIKeyboardDidShowNotification
và UIKeyboardFrameBeginUserInfoKey
để có được kích thước của bàn phím) khi chuyển một cái nhìn để một trường văn bản mà có thể đã được che khuất bởi một bàn phím có thể nhìn thấy. Tham khảo:
How to make a UITextField move up when keyboard is present?
iOS Library: Text, Web, and Editing Programming Guide for iOS --> Managing the Keyboard
iOS Library: Scroll View Programming Guide for iOS --> Creating and Configuring Scroll Views
BugVC.h
#import <UIKit/UIKit.h>
@interface BugVC : UIViewController <UITextFieldDelegate> {
UITextField *oneTF;
UITextField *twoTF;
UITextField *normalTF;
UILabel *keyboardWillShowNotificationL;
UILabel *keyboardDidShowNotificationL;
}
- (void) oneReturnTouchDown:(id)sender;
- (void) twoReturnTouchDown:(id)sneder;
- (void) keyboardWillShowNotification:(NSNotification *)aNotification;
- (void) keyboardDidShowNotification:(NSNotification *)aNotification;
@end
BugVC.m
#import "BugVC.h"
@implementation BugVC
- (id) init
{
if (!(self = [super init]))
{
return self;
}
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// One text field with 100 height keyboard
oneTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
oneTF.borderStyle = UITextBorderStyleRoundedRect;
oneTF.text = @"100";
oneTF.delegate = self;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Custom input view for the above text field
UIView *oneInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
oneInputView.backgroundColor = [UIColor redColor];
UIButton *oneReturnB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
oneReturnB.frame = CGRectMake(10, 10, 300, 30);
[oneReturnB setTitle:@"return" forState:UIControlStateNormal];
[oneReturnB addTarget:self
action:@selector(oneReturnTouchDown:)
forControlEvents:UIControlEventTouchDown];
[oneInputView addSubview:oneReturnB];
oneTF.inputView = oneInputView;
[oneInputView release];
[self.view addSubview:oneTF];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Two text field with 200 height keyboard
twoTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 30)];
twoTF.borderStyle = UITextBorderStyleRoundedRect;
twoTF.text = @"200";
twoTF.delegate = self;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Custom input view for the above text field
UIView *twoInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
twoInputView.backgroundColor = [UIColor blueColor];
UIButton *twoReturnB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
twoReturnB.frame = CGRectMake(10, 10, 300, 30);
[twoReturnB setTitle:@"return" forState:UIControlStateNormal];
[twoReturnB addTarget:self
action:@selector(twoReturnTouchDown:)
forControlEvents:UIControlEventTouchDown];
[twoInputView addSubview:twoReturnB];
twoTF.inputView = twoInputView;
[twoInputView release];
[self.view addSubview:twoTF];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// normal text field with normal keyboard (216 height keyboard)
normalTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 90, 300, 30)];
normalTF.borderStyle = UITextBorderStyleRoundedRect;
normalTF.text = @"normal";
normalTF.delegate = self;
[self.view addSubview:normalTF];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Label that displays the keyboard height from keyboardWillShowNotification
keyboardWillShowNotificationL = [[UILabel alloc] initWithFrame:CGRectMake(10, 130, 300, 30)];
keyboardWillShowNotificationL.font = [UIFont systemFontOfSize:14];
keyboardWillShowNotificationL.text = @"keyboardWillShowNotification kbHeight:";
[self.view addSubview:keyboardWillShowNotificationL];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Label that displays the keyboard height from keyboardDidShowNotification
keyboardDidShowNotificationL = [[UILabel alloc] initWithFrame:CGRectMake(10, 170, 300, 30)];
keyboardDidShowNotificationL.font = [UIFont systemFontOfSize:14];
keyboardDidShowNotificationL.text = @"keyboardDidShowNotification kbHeight:";
[self.view addSubview:keyboardDidShowNotificationL];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Register for keyboard notifications.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShowNotification:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardDidShowNotification:)
name:UIKeyboardDidShowNotification object:nil];
return self;
}
- (void) dealloc
{
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Deregister for keyboard notifications
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardDidShowNotification object:nil];
[oneTF release];
[twoTF release];
[normalTF release];
[keyboardWillShowNotificationL release];
[keyboardDidShowNotificationL release];
[super dealloc];
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void) oneReturnTouchDown:(id)sender
{
[oneTF.delegate textFieldShouldReturn:oneTF];
}
- (void) twoReturnTouchDown:(id)sneder
{
[twoTF.delegate textFieldShouldReturn:twoTF];
}
- (void) keyboardWillShowNotification:(NSNotification *)aNotification
{
NSDictionary *info = [aNotification userInfo];
CGFloat kbHeight =
[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
NSString *string = [[NSString alloc] initWithFormat:@"keyboardWillShowNotification kbHeight: %.2f", kbHeight];
NSLog(@"%@", string);
keyboardWillShowNotificationL.text = string;
[string release];
}
- (void) keyboardDidShowNotification:(NSNotification *)aNotification
{
NSDictionary *info = [aNotification userInfo];
CGFloat kbHeight =
[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
NSString *string = [[NSString alloc] initWithFormat:@"keyboardDidShowNotification kbHeight: %.2f", kbHeight];
NSLog(@"%@", string);
keyboardDidShowNotificationL.text = string;
[string release];
}
@end
Cảm ơn bạn đã chỉ ra rằng tôi nên sử dụng UIKeyboardFrameEndUserInfoKey. Khi tôi nhìn vào tài liệu ban đầu, tôi không rõ sự khác nhau giữa UIKeyboardFrameBeginUserInfoKey và UIKeyboardFrameEndUserInfoKey là gì. Cảm ơn lời giải thích. – mmorris
Tuyệt vời!Đây là một trường hợp tối nghĩa, mà tôi vừa chạy vào trộn bàn phím tiêu chuẩn và bộ chọn ngày và may mắn tìm thấy bài đăng của bạn. UIKeyboardFrameEndUserInfoKey là thông tin quan trọng cần thiết !!! –