2011-10-19 38 views
0

我正在使用IOS開發人員庫中的示例來管理鍵盤。 Text, Web and Edition Programming GuideIOS管理鍵盤 - 向上移動UIScrollView內容

我已經使用IB創建了一個視圖。它是一個簡單的UI,它有一個UIScrollView,UITextView,UIButton和一個UITextField。我將UIScrollView放在我的視圖上,然後將所有其他控件添加爲此滾動視圖的子項。滾動視圖通過IBOutlet「scrollView」暴露給viewcontroller。

下面的代碼在用戶設置焦點到textField時執行,但我從不會看到滾動條出現,滾動條的內容也不會移動。我應該能夠默認看到滾動條嗎?有人能告訴我我錯過了什麼嗎?

-(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]; 
    } 
} 

同樣,我直接從鏈接中的IOS編程指南中獲取此代碼。 UI佈局看起來像一個基本的聊天窗口。我想在軟鍵盤可見時將「輸入」字段向上移動。謝謝! 更新

看來我需要添加一些填充來實際看到位於滾動視圖底部的控件。

CGPoint scrollPoint = CGPointMake(0.0, (activeField.frame.origin.y - kbSize.height) + 10.0); 

我怎麼沒看到滾動條?

+0

你的問題標題建議你問 「我如何將我的scrollcontent了?」而你的問題實際上是「爲什麼滾動條指標不可見?」或「如何使滾動條指示器可見?」。你可能想編輯它。 – Aberrant

回答

0

滾動條應僅在用戶交互時顯示。這不是這種情況,因爲你以編程方式設置滾動視圖的插入。

如果你想顯示滾動條,我相信UIScrollView定義了flashScrollIndicators方法,即應該顯示滾動條。

1

IF YOU HAVE父視圖AS SCROLL就用:UITextFielddelegate和設置方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    if(textField == self.txtUserName) 
    { 
     [self.txtPassword becomeFirstResponder]; 
    } 
    else if(textField == self.txtPassword){ 
     [self.txtPassword resignFirstResponder]; 
     CGPoint bottomOffset = CGPointMake(0, 0); 
     [scrView setContentOffset:bottomOffset animated:YES]; 
     [textField resignFirstResponder]; 
    } 
    return YES; 
} 

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    if (textField == self.txtUserName) 
    { 
     CGPoint bottomOffset = CGPointMake(0, 80); 
     [scrView setContentOffset:bottomOffset animated:YES]; 
    } 
    if (textField == self.txtPassword) 
    { 
     CGPoint bottomOffset = CGPointMake(0, 135); 
    [scrView setContentOffset:bottomOffset animated:YES]; 
    } 

}