2013-10-07 122 views
0

我有一個具有文本字段的視圖。還配置了鍵盤以完成按鈕以隱藏鍵盤。我已將代理附加到我的txtfield,以便在顯示鍵盤時向上滾動。它的工作正常,但當我點擊完成隱藏鍵盤,視圖保持滾動和用戶不得不手動向後滾動它..任何想法/指針如何解決此問題..如何在隱藏鍵盤時向下滾動視圖

以下是我的代碼以實現滾動功能..

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    keyboardIsShown = NO; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillShow:) 
               name:UIKeyboardWillShowNotification 
               object:self.view.window]; 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillHide:) 
               name:UIKeyboardWillHideNotification 
               object:self.view.window]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 

    [super viewDidDisappear:animated]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillShowNotification 
                object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillHideNotification 
                object:nil]; 
} 


- (void)keyboardWillShow:(NSNotification *)n 
{ 
    if (keyboardIsShown) { 
     return; 
    } 

    NSDictionary* userInfo = [n userInfo]; 

    // get the size of the keyboard 
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    // resize the noteView 
    CGRect viewFrame = self.scrollView.frame; 
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView. 
    viewFrame.size.height -= (keyboardSize.height - kTabBarHeight); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    // The kKeyboardAnimationDuration I am using is 0.3 
    [UIView setAnimationDuration:kKeyboardAnimationDuration]; 
    [self.scrollView setFrame:viewFrame]; 
    [UIView commitAnimations]; 

    scrollView.contentSize = formView.frame.size; 


    keyboardIsShown = YES; 
} 

- (void)keyboardWillHide:(NSNotification *)n 
{ 
    NSDictionary* userInfo = [n userInfo]; 

    // get the size of the keyboard 
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 


    // resize the scrollview 
    CGRect viewFrame = self.scrollView.frame; 
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView. 
    viewFrame.size.height += (keyboardSize.height - kTabBarHeight); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    // The kKeyboardAnimationDuration I am using is 0.3 
    [UIView setAnimationDuration:kKeyboardAnimationDuration]; 
    [self.scrollView setFrame:viewFrame]; 
    [UIView commitAnimations]; 

    keyboardIsShown = NO; 
} 
+1

你可以通過刪除所有這樣註冊的觀察者來縮短代碼: [[NSNotificationCenter defaultCente r] removeObserver:self]; –

回答

0

合併如果你只是想回動畫頂部滾動。

-(void)keyboardWillHide:(NSNotification *)notification { 
     UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
     CGPoint top = CGPointMake(0, 0); 
     [self.scrollView setContentOffset:top animated:YES]; 
     self.scrollView.scrollIndicatorInsets = contentInsets; 
} 

不需要在上面實現的keyboardIsShown實例變量

+0

謝謝@kyle C ..我添加了CGPoint top = CGPointMake(0,-65);和它的工作。希望Y座標的硬編碼不會產生任何問題?任何建議plz。 –

0

試試這個:

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 216, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
    CGRect frame = CGRectMake(0, textFieds.frame.origin.y + kOtherViewsHeightWhichMightNotBeInSameViewAsScrollView, 10, 10); 

    [self.scrollView scrollRectToVisible:frame] animated:YES]; 
} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
    return YES; 
} 

不要忘記設置您的視圖控制器爲您的文本字段

的代表
相關問題