我在這個問題上花了大約2天時間,經歷了幾次堆棧溢出/在線帖子,似乎無法找到答案。鍵盤隱藏UITextFields。打字後開始滾動
我有6個UITextFields。當鍵盤出現時底部兩個隱藏。然而,當我開始打字:
在一個4" 畫面(iPhone 5):該文本框的滾動和隱藏導航欄後面
在3.5" 屏幕(iPhone 3GS):該文本框的滾動在導航欄的正下方,這是它應該在的位置。
當用戶點擊UITextFields時,我希望UITextFields向上滾動並最終在導航欄RIGHT AWAY之前結束。這樣,該字段出現並準備好輸入,並且不會隱藏等待用戶在滾動之前開始鍵入。
我不確定這是否相關,但我有UIControllerView和裏面,它有一個containerView。容器視圖不覆蓋整個屏幕,並從X開始:68 Y:7寬度:237高度:351.在ContainerView中,我有UITcrollView和UITextFields。 containerView也有自己的NavigationBar。在iPhone 5(4「屏幕)上滾動時,TextField隱藏在containerView內的NavigationBar後面。在iPhone 3GS(3.5」屏幕)上,UITextfield正好出現在NavigationBar下方。
下面是我從蘋果文檔遵循代碼:
.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;
}
UIScrollView是我用於這個,它工作正常,只需將文本框添加到它並根據需要滾動。 –