2013-01-05 134 views
2

我有多個文本字段,當我專注於文本框時,它會自動向上滾動並且鍵盤隱藏文本字段。當焦點和鍵盤隱藏文本字段時滾動到文本字段

任何想法如何在點擊時將文本字段滾動到焦點字段?

- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillBeHidden:) 
               name:UIKeyboardWillHideNotification object:nil]; 

} 
- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    activeField = textField; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    activeField = nil; 
} 
// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
} 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
    scrollView.contentInset = contentInsets; 
    scrollView.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); 
     [scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

回答

1

,你貼我覺得是從蘋果的文檔,它假設一個基本觀點分層結構,同時一個UIScrollView(或其子類之一,像UITableView中)的一個充滿整個屏幕的代碼片段。如果您的視圖佈局更加複雜,或者您需要支持多個方向,則文本字段將不會滾動爲可見,因爲矩形計算會出錯。您需要稍微調整一下代碼,我的建議是您以這種方式處理問題:

滾動視圖的新contentInsent高度應該等於鍵盤和滾動視圖之間的相交矩形的高度

在代碼:我已經使用了方便的UIScrollView的scrollRectToVisible功能抽象最終滾動操作儘可能

- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    CGRect kbRawRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];  
    CGRect scrollViewFrame = [self.scrollView.window convertRect:self.scrollView.frame fromView:self.scrollView.superview]; 

    // Calculate the area that is covered by the keyboard 
    CGRect coveredFrame = CGRectIntersection(scrollViewFrame, kbRawRect); 
    // Convert again to window coordinates to take rotations into account 
    coveredFrame = [self.scrollView.window convertRect:self.scrollView.frame fromView:self.scrollView.superview]; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, coveredFrame.size.height, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    CGRect activeFieldRect = [self.activeField convertRect:self.activeField.bounds toView:self.scrollView]; 
    [self.scrollView scrollRectToVisible:activeFieldRect animated:YES]; 
} 

通知。

+0

KeyboardWasShown在文本框焦點時未被調用。 – user1688346

+0

您是否在viewDidLoad方法中調用了registerForKeyboardNotifications? –

+0

是的。我確實註冊了它。 – user1688346

相關問題