1

テキスト フィールドとテキスト ビューを含む UIScrollView があります。キーボードが存在するときにテキスト フィールドを上に移動して、キーボードがテキスト フィールドを覆わないようにするコードがあります。このコードは、縦表示でうまく機能します。

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    if(textField) {
        [textField resignFirstResponder];
    }
    return NO;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField == self.nameField) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x,   (self.view.frame.origin.y - 90), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.nameField) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 90), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

-(void)textViewDidBeginEditing:(UITextView *)textField {
    if (textField == self.questionField) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 200), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

-(void)textViewShouldEndEditing:(UITextView *)textField {
    if (textField == self.questionField) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationBeginsFromCurrentState:YES];
        UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;

        self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 200), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

iPhone シミュレーターを回転させて横向きにすると、(驚くことではありませんが) 機能しません。横向きと縦向きの両方のビューで入力しているときに、テキスト フィールドを上に移動して表示するにはどうすればよいですか?

また、キーボードが表示されているときに横向きから縦向きに回転すると、キーボードを閉じた後、スクロール ビューが元の位置に並ぶのではなく、画面の下に移動します。どうすればこれを回避できますか?

4

1 に答える 1

2

Apple の文書「キーボードの管理」の「キーボードの下にあるコンテンツの移動」という見出しの下に、これを行う適切な方法が示されています。

基本的にコンテンツをスクロール ビューに配置し、UIKeyboardDidShowNotificationおよびUIKeyboardWillHideNotification通知を使用してコンテンツ オフセットを調整します。

ただし、コードはやや不完全です。キーボード サイズの計算は、ランドスケープ モードでは機能しません。これを修正するには、これを置き換えます。

CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

これとともに:

// Works in both portrait and landscape mode
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect toView:nil];

CGSize kbSize = kbRect.size;
于 2013-08-07T18:02:25.330 に答える