Trước hết tạo ra một cái nhìn (tôi đã làm nó trong một riêng biệt nib file và nạp nó theo cách này):
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ReducedNumericKeyboardView"
owner:self
options:nil];
keyBView = (ReducedNumericKeyboardView*)[views objectAtIndex:0];
Sau đó, tôi đặt nó như là cái nhìn đầu vào cho các trường văn bản nơi mà tôi muốn sử dụng nó (và trên thực tế, đây là trả lời ngắn gọn cho câu hỏi của bạn;)) :
[self.propertyEditor setInputView:keyBView];
Khi nhấp vào trường tôi làm di chuyển các pup view (nếu cần thiết) để không bao gồm lĩnh vực:
CGRect textFieldRect = [self.tableViewController.view.window convertRect:propertyEditor.bounds fromView:propertyEditor];
CGRect viewRect = [self.tableViewController.view.window convertRect:self.tableViewController.view.bounds fromView:self.tableViewController.view];
CGFloat midLine = textFieldRect.origin.y+.5*textFieldRect.size.height;
CGFloat numerator = midLine - viewRect.origin.y - MINIMUM_SCROLL_FRACTION*viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)*viewRect.size.height;
CGFloat heightFraction = MIN(1, MAX(0, numerator/denominator));
animateDistance = floor(PORTRAIT_USER_INPUT_VIEW_HEIGHT*heightFraction);
CGRect viewFrame = self.tableViewController.view.frame;
viewFrame.origin.y -= animateDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:USER_INPUT_ANIMATION_DURATION];
[self.tableViewController.view setFrame:viewFrame];
[UIView commitAnimations];
Khi chỉnh sửa xong, tôi làm di chuyển nhìn xuống:
CGRect viewFrame = self.tableViewController.view.frame;
viewFrame.origin.y += animateDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:USER_INPUT_ANIMATION_DURATION];
[self.tableViewController.view setFrame:viewFrame];
[UIView commitAnimations];
Các khó khăn tôi sử dụng được thiết lập như sau:
static const CGFloat USER_INPUT_ANIMATION_DURATION = 0.3;
static const CGFloat PORTRAIT_USER_INPUT_VIEW_HEIGHT = 180;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.1;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.2;
Tôi đã trả lời một câu hỏi rất giống nhau (được liên kết đến trong câu trả lời của tôi dưới đây: http://stackoverflow.com/a/13351686/937822) tạo một bàn phím tùy chỉnh. Tôi đã làm cho nó mã nguồn mở và đặt nó trên Github cho tất cả để sử dụng, và gần đây đã thêm một ví dụ với một bàn phím hex. Nếu bạn chỉ muốn a-c và 1-3, bạn chỉ cần xóa các nút bổ sung! – lnafziger