1

これは、私のViewControllerがストーリーボードでどのように見えるかです:

ここに画像の説明を入力

@interface SettingViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, NSURLConnectionDelegate> { }

このリンクでシウンの回答を見つけました。しかし、それはまさに私が探しているものではありません。

このテキスト フィールドがキーボードの後ろに消えることがあります。

textField をクリックすると、キーボードがポップアップします。

TextField がキーボードの上部に表示されるようにします。

今まで適切な解決策を見つけることができませんでした。私たちを手伝ってくれますか?

4

1 に答える 1

2

これを行う直接的な方法はありません。キーボード通知を聞いてから、キーボードの高さを見つけてテキスト フィールドをキーボードの上に移動する必要があります。

// 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);
    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];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}
于 2012-11-12T22:48:11.927 に答える