2011-12-15 10 views
3

私はiOSプログラミングが初めてで、キーボードによって隠されたUITextFieldを編集するときにUIScrollViewを移動するのに問題があります。コードはApple's documentationからまっすぐですが、何らかの理由で機能しません。キーボードがアクティブなUITextFieldをカバーしているときにUIScrollViewがスクロールしない(appleの例を使用)

私は、通知が正しく渡されているように見える(つまり、「Viewはサイズを変更する必要がありますが、activeFieldがキーボードの下にあるtextFieldである場合のみ)」とスクロールポイントが正しく設定されているスクロールビューはまだ動きません。また、私はコードがドキュメントからまっすぐであると見て(のViewControllerがscrollViewだけでなく、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]; 
    NSLog(@"%@",@"view should resize"); 
} 
} 

委任パターンが正しいことを合理的に確信している、私はおそらく、単純な何かが欠けています。誰かがチェックするものの方向に私を指すことができますか?

+0

FYI:あなたは(「ビューのサイズを変更する必要があり、」@) 'のNSLogに最後の行を変更することができます;' – PengOne

+0

http://stackoverflow.com/a/672003/19679 –

+0

適切な代議員を設定しましたか? – jakenberg

答えて

1

アップルの例では、スクロールビューのコンテンツサイズが明示的に設定されていないため、デフォルトのコンテンツサイズ(0、0)が使用されているというバグがあります。私は私のビューコントローラでこのコードを追加することで、この問題を修正:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Set the scroll view's content size to be the same width as the 
    // application's frame but set its height to be the height of the 
    // application frame minus the height of the navigation bar's frame 
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 
    CGRect navigationFrame = [[self.navigationController navigationBar] frame]; 
    CGFloat height = applicationFrame.size.height - navigationFrame.size.height; 
    CGSize newContentSize = CGSizeMake(applicationFrame.size.width, height); 

    ((UIScrollView *)self.view).contentSize = newContentSize; 
} 
関連する問題