0

ビューの下部にテキストフィールドがあります。クリックして編集すると、キーボードがそれを覆い隠し、入力内容が表示されなくなります。このテキストフィールドが編集されているときはいつでもビューを再調整するメソッドを作りたいです。

インターフェイスビルダーを使用してテキストフィールドを宣言した場合、リンク(Ctrlキーを押しながらクリックしてドラッグ)するだけで済みます。しかし、私はコードでテキストフィールドを宣言しているので、それにメソッドを割り当てる方法を考えています。

私がやりたいことの擬似コード:

if (the text field is currently being edited){
    call method: adjust view
}
4

4 に答える 4

3

次のように、UITextFieldDelegateを.hに実装します。

@interface ShippingInfoViewController : UIViewController < UITextFieldDelegate>

textFieldのデリゲートをに設定してくださいself

次に、次のような多くのメソッドにアクセスできます。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

-(void) textFieldDidBeginEditing:(UITextField *)textField{

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

- (void)textFieldDidEndEditing:(UITextField *)textField {

と他の多く。

アップルリファレンス

于 2012-07-16T17:24:51.233 に答える
1

テキストフィールドを作成するときは、そのデリゲートを自分自身に設定します。

[textField setDelegate:self];

次に、デリゲートメソッドを実装します

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    //adjust view here
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    //move view back to correct position here
}

UIKeyboardWillShow/UIKeyboardWillHideまたはUIKeyboardDidShow/UIKeyboardDidHideメッセージに登録して、そのための関数を実装することもできます。

于 2012-07-16T17:24:30.247 に答える
0

次の関数を使用します。

- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


- (void)keyboardWillShow:(NSNotification *)notification
{  
  [self moveTextViewForKeyboard:notification up:YES];
}


- (void)keyboardWillHide:(NSNotification *)notification
{
  [self moveTextViewForKeyboard:notification up:NO];
}


- (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up {
   NSDictionary *userInfo = [notification userInfo];
   NSTimeInterval animationDuration;
   UIViewAnimationCurve animationCurve;
   CGRect keyboardRect;

  [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
  animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

  [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
  [UIView setAnimationDuration:animationDuration];
  [UIView setAnimationCurve:animationCurve];

 if (up == YES) {
    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.noteTextView.frame;
    originalTextViewFrame = self.noteTextView.frame;
    newTextViewFrame.size.height = keyboardTop - self.noteTextView.frame.origin.y - 10;

    self.noteTextView.frame = newTextViewFrame;
 } else {
    // Keyboard is going away (down) - restore original frame
    self.noteTextView.frame = originalTextViewFrame;
 }

  [UIView commitAnimations];
}

originalTextViewFrameに静的変数を使用します:

static CGRect originalTextViewFrame;
于 2012-07-16T17:49:48.790 に答える
0

テキストビューにデリゲートを割り当てる必要があります。

[textView setDelegate: self]

私が想定しているのは、プロトコルselfに準拠する必要があるViewControllerです。テキストビューは、編集サイクルを開始するたびに/を呼び出します。UITextViewDelegatetextViewShouldBeginEditing:textViewWillBeginEditing

于 2012-07-16T17:23:57.930 に答える