我在这个问题上花了大约 2 天的时间浏览了几篇堆栈溢出/在线帖子,但似乎找不到答案。
我有 6 个 UITextField。当键盘出现时,底部的两个隐藏。但是,当我开始输入时:
在 4" 屏幕上 (iPhone 5):文本字段向上滚动并隐藏在导航栏后面。
在 3.5 英寸屏幕上 (iPhone 3GS):文本字段在导航栏正下方向上滚动,这是它应该在的位置。
当用户单击 UITextFields 时,我希望 UITextFields 向上滚动并在导航栏之前结束,马上。这样,该字段就会出现并准备好输入,并且不会隐藏,等待用户在向上滚动之前开始输入。
我不确定这是否相关,但我有 UIControllerView,在里面,它有一个 containerView。containerView 不覆盖整个屏幕,从 X:68 Y:7 开始 宽度:237 高度:351。在 ContainerView 内部,我有 UIScrollView 和 UITextFields。containerView 也有自己的 NavigationBar。在 iPhone 5(4" 屏幕)上,向上滚动时,TextFields 隐藏在 containerView 内的 NavigationBar 后面。在 iPhone 3GS(3.5" 屏幕)上,UITextfield 出现在 NavigationBar 的正下方,它们应该如此。
这是我从 Apple Docs 中遵循的代码:
.h
@interface ContactViewController : UIViewController <UITextFieldDelegate>
{
UITextField *activeField;
}
.m
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0.0);
pageScrollView.contentInset = contentInsets;
pageScrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[pageScrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
pageScrollView.contentInset = contentInsets;
pageScrollView.scrollIndicatorInsets = contentInsets;
}
我从 Apple Doc 获得了代码:http: //developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement