0

最後に多くのテキストフィールドとボタンがあるiPadにフォームがあります。アクティブなときにキーボードの下に表示されるフィールドがいくつかあります。キーボードの背後にある非表示のテキスト フィールドを表示するために、次のコードを使用しています。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
_scrollView.frame=CGRectMake(0, 0, 1024, 655);
[self animateTextField:textField up:NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
CGPoint temp = [textField.superview convertPoint:textField.frame.origin toView:nil];
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait)
{
    // NSLog(@"portrait");
    if(up)
    {
        int moveUpValue = temp.y+textField.frame.size.height;
        animatedDis = 264-(1024-moveUpValue-5);
    }
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
{
    if(up)
    {
        int moveUpValue = 1004-temp.y+textField.frame.size.height;
        animatedDis = 264-(1004-moveUpValue-5);
    }
}
else if(orientation == UIInterfaceOrientationLandscapeLeft)
{
    if(up)
    {
        int moveUpValue = temp.x+textField.frame.size.height;
        animatedDis = 352-(768-moveUpValue-5);
    }
}
else
{
    if(up)
    {
        int moveUpValue = 768-temp.x+textField.frame.size.height;
        animatedDis = 352-(768-moveUpValue-5);
        _scrollView.frame = CGRectMake(0, 0, 1024, 655-240);
    }

}
if(animatedDis>0)
{
    const int movementDistance = animatedDis;
    const float movementDuration = 0.3f;
    int movement = (up ? -movementDistance : movementDistance);
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    if (orientation == UIInterfaceOrientationPortrait)
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    else if(orientation == UIInterfaceOrientationLandscapeLeft)
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    else
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    [UIView commitAnimations];
}
}

スクロールビューも使用しています。私の問題は、キーボードがアクティブなときにボタンを押すと、次の画面に移動することです。戻ると、キーボードがアクティブになり、前のアニメーションが設定されます。キーボードを非表示にすると、ビュー全体が下にスクロールし、黒い部分が上に残ります。この状況をどのように処理しますか?

4

1 に答える 1

0

わかった。多くの研究の後、私は簡単な方法を見つけました。それは通知の使用です。私のviewDidLoadでは、これら2つのキーボード通知を追加しました。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name: UIKeyboardDidShowNotification object:nil];

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

これらは 2 つのセレクター メソッドです。

-(void)keyboardWasShown:(NSNotification *)aNotification
{

if (displayKeyboard==YES) {
    return;
}
NSDictionary* info = [aNotification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
//NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;

NSLog(@"kbw====%fkbh====%f",keyboardSize.width,keyboardSize.height);

offset = _scrollView.contentOffset;

CGRect viewFrame = _scrollView.frame;

NSLog(@"svw====%fsvh===%f",viewFrame.size.width,viewFrame.size.height);
viewFrame.size.height -= keyboardSize.height-49;
NSLog(@"new view hgt =====%f",viewFrame.size.height);
_scrollView.frame = viewFrame;

CGRect textFieldRect = [activeField frame];
textFieldRect.origin.y += 10;
[_scrollView scrollRectToVisible: textFieldRect animated:YES];
displayKeyboard = YES;
}
-(void)keyboardWillBeHidden:(NSNotification *)aNotification
{

if (!displayKeyboard) {
    return; 
}

_scrollView.frame = CGRectMake(0, 0, 1024, 655);
_scrollView.contentOffset =offset;
displayKeyboard = NO;
}

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField;
return YES;
}

displayKeyboard、offset、activeField は .h ファイルで宣言されています。

また、viewDidDisappear:animated の通知を削除することを忘れないでください

この方法は前述の方法とはかなり異なりますが、この方法では、uikeyboard がアクティブなときにクラス間を移動するときに上部に黒い部分が残りません。

また、私が気づいたUIKeyboardBoundsUserInfoKeyのは、キーボードの正しい幅と高さを取得するために使用されていた廃止されたものを使用した場合です(私は横向きモードでのみ作業しています)。私が使用したときUIKeyboardFrameBeginUserInfoKey、幅と高さの値が交換されました。私はまだこの問題を理解しようとしています。

また、以前はキーボードが表示されていたときに、その上に 49px の固定スペースが追加されていました。それが私のタブバーの高さであると想定したため、49 を引きました。

于 2012-06-15T11:35:51.703 に答える