0

私はここでAppleの例を試しました:

http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

しかし、うまくいかないようですので、途中で明らかに何かが欠けていて、何がわからないのです。

私はViewController.hを含んでいます:

@interface PreferencesViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, defaultLocationChoice, UITextFieldDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) NSString *defaultLocation;
@property (nonatomic, retain) NSString *defaultTestType;
@property (nonatomic, assign) id <defaultLocationChoice> locationDelegate;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) IBOutlet UITextField *locationnameTextField;
@property (nonatomic, strong) IBOutlet UITextField *locationaddressTextField;
@property (strong, nonatomic) IBOutlet UITableView *scrollView;
@property (nonatomic, strong) IBOutlet UITextField *activeField;

.mファイルにはすべてSynthesizedがあります。

私はAppleのコードがViewController.mに続くようにしています

- (void)viewDidLoad
{
    NSUserDefaults *sharedPref = [NSUserDefaults standardUserDefaults];
    defaultLocation =[sharedPref stringForKey:@"defaultLocation"];
    defaultTestType =[sharedPref stringForKey:@"defaultTestType"];

    [self registerForKeyboardNotifications];


    self.navigationItem.title = @"Preferences";

    [super viewDidLoad];

}

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

- (void)textFieldDidBeginEditing:(UITextField *)textField

{
    activeField = textField;
}



- (void)textFieldDidEndEditing:(UITextField *)textField

{
    activeField = nil;
}

TextFieldsを含むセルは、CellForRowAtIndexPathで次のように処理されます(完全なコードは表示されていません)。

  case 2:
            switch (indexPath.row) {
                case 0:

                    prefCell.textLabel.text = @"";
                    prefCell.accessoryType = UITableViewCellAccessoryNone;
                    prefCell.highlighted = NO;
                    locationnameTextField.frame = CGRectMake(5, 12, 300, 30);
                    locationnameTextField.font = [UIFont systemFontOfSize:13.0f];
                    locationnameTextField.placeholder = @"Enter location name i.e. New York, NY";
                    locationnameTextField.delegate = self;
                    locationnameTextField.returnKeyType = UIReturnKeyDone;
                    [prefCell.contentView addSubview:locationnameTextField];

                    break;
                case 1:
                    prefCell.textLabel.text = @"";
                    prefCell.accessoryType = UITableViewCellAccessoryNone;

                    locationaddressTextField.frame = CGRectMake(5, 12, 300, 30);
                     locationaddressTextField.font = [UIFont systemFontOfSize:13.0f];
                    locationaddressTextField.placeholder = @"Enter location address i.e. mcs.newyork.com";
                    locationaddressTextField.clearsOnBeginEditing = YES;
                    locationaddressTextField.delegate = self;
                    locationaddressTextField.returnKeyType = UIReturnKeyDone;

                    [prefCell.contentView addSubview:locationaddressTextField];

                    break;
            }

アプリを実行すると、キーボードがポップアップし、何も起こりません。

self.view.frameAppleの例はビュー用であるため、上記で変更した唯一のコードはself.tableView.frame効果がありませんでした。

ScrollViewがなかったので、追加としてScrollViewを追加しました。TableViewが埋め込まれている他のViewControllerは、スクロールするのにScrollViewを必要としません。

階層は次のとおりです。

ここに画像の説明を入力してください

どんな助けでも素晴らしいでしょう、ありがとう

4

3 に答える 3

1

次のようにする必要があります。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
 [yourTextField resignFirstResponder];
}
于 2013-03-11T13:02:08.527 に答える
0

テキストフィールドのスーパービューに気づいたとき、同じ問題で頭を悩ませていました。私が正しければ、テーブルビューのセル内にテキストフィールドがあります。

今、あなたのkeyboardWasShown:メソッドでは、アクティブなテキストフィールドがキーボードの下に隠されているかどうかを次のようにチェックしています:

if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];
} 

あなたのif場合、条件が真になることは決してありません。アクティブなテキスト フィールドのスーパービューはセルであるactiveField.frame.originため、UIView ではなく、UITableViewCell を参照してポイントを返します。

したがって、次のように、アクティブなテキスト フィールドを含むセルがキーボードの下に隠れているかどうかを確認する必要があります。

CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, cellForActiveField.frame.origin) ) {
    CGPoint scrollPoint = CGPointMake(0.0, cellForActiveField.frame.origin.y-kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];
} 

アクティブなテキスト フィールドを含むセルを見つけるには、メソッドtagで textfield のプロパティを設定し、tableView:cellForIndexPath:メソッドでタグを確認する必要がありますtextFieldDidBeginEditing:

詳細については、この質問も参照してください。

お役に立てれば。

于 2013-05-11T19:06:55.223 に答える
0

見つけたのでTPKeyboardAvoidingを使っています

それはうまく機能しており、セットアップは非常に簡単です。

  1. UIScrollView をビュー コントローラーの xib に追加します。
  2. スクロール ビューのクラスを TPKeyboardAvoidingScrollView に設定します (ID インスペクターを使用して、まだ xib 内にあります)。
  3. そのスクロールビュー内にすべてのコントロールを配置します
于 2014-02-12T20:36:49.983 に答える