2012-01-25 63 views
2

我嘗試過,但視圖太小,無法向上滾動。我如何滾動更多?如何在目標c中出現鍵盤時滾動視圖?

// Call this method somewhere in your view controller setup code. 
- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification 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, 0.0, kbSize.height, 0.0); 
scroll.contentInset = contentInsets; 
scroll.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, EPostaText.frame.origin)) { 
    CGPoint scrollPoint = CGPointMake(0.0, EPostaText.frame.origin.y-(aRect.size.height)); 
    [scroll setContentOffset:scrollPoint animated:YES]; 
} 
} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
scroll.contentInset = contentInsets; 
scroll.scrollIndicatorInsets = contentInsets; 
} 

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

} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
EPostaText = nil; 
} 
+0

http://stackoverflow.com/questions/2307200/iphone-keyboard-hides-textfield/6452886#6452886 – tipycalFlow

回答

2

最好的方法 - 這是調整滾動視圖容器可見區域,並使用此:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated 
0

因爲我發現,我用TPKeyboardAvoiding

這是偉大的工作,並非常容易設置:

  1. 將UIScrollView添加到您的視圖控制器的xib中
  2. 設置滾動視圖對TPKeyboardAvoidingScrollView類(在廈門國際銀行仍然 ,通過身份檢查)
  3. 將所有的控件,滾動型

祝你好運之內!

0

這是我的代碼,希望它能幫助你。它工作正常,如果你有很多文本域

CGPoint contentOffset; 
bool isScroll; 
- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    contentOffset = self.myScroll.contentOffset; 
    CGPoint newOffset; 
    newOffset.x = contentOffset.x; 
    newOffset.y = contentOffset.y; 
    //check push return in keyboar 
    if(!isScroll){ 
     //180 is height of keyboar 
     newOffset.y += 180; 
     isScroll=YES; 
    } 
    [self.myScroll setContentOffset:newOffset animated:YES]; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField{ 
    //reset offset of content 
    isScroll = NO; 
    [self.myScroll setContentOffset:contentOffset animated:YES]; 
    [textField endEditing:true]; 
    return true; 
} 
相關問題