0

サブビューに viewLogin として 2 つのテキストフィールドがあります。テキストキーボードを入力しようとすると、それらが非表示になります。viewLoginを移動することでその問題を解決しました。コードは...

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

    UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

        viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/9, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
    else {
        viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
}

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

    UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
        viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/2.5, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
    else {
        viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
}

ここで直面している問題は、最初に向きを横向きに変更して textField をクリックすると正常に動作しますが、最初に縦向きモードで textField をクリックしてから横向きに変更すると、左/右が機能しません..

これを解決するのを手伝ってくれる人もいます....

前もって感謝します

4

4 に答える 4

1

これは Apple が使用するものです ( KeyboardAccessory の例を見てください): まず、キーボード通知に登録します。viewDidLoad

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

で登録を解除してくださいviewDidUnload

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

次に、これらのメソッドを実装に追加します。

- (void)keyboardWillShow:(NSNotification *)notification 
{
    NSDictionary *userInfo = [notification userInfo];

    NSValue *keyboardEndFrameValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [keyboardEndFrameValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.view.bounds;
    newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    [UIView animateWithDuration:animationDuration
                     animations:^{
                         self.textView.frame = newTextViewFrame;
                     }];
}


- (void)keyboardWillHide:(NSNotification *)notification 
{
    NSDictionary *userInfo = [notification userInfo];

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    [UIView animateWithDuration:animationDuration
                     animations:^{
                         self.textView.frame = self.view.bounds;
                     }];
}

使用していますが、向きの変更に問題はありません。

于 2012-01-04T09:34:27.783 に答える
0

textFieldDidBeginEditing メソッドは、最初のケースでは後で呼び出され、2 番目のケースでは呼び出される前に呼び出されるため、テキストフィールド編集の shouldChangeCharactersInRange でコーディングする必要があります。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

if (orientation == UIDeviceOrientationLandscapeLeft || orientation ==  UIDeviceOrientationLandscapeRight) {
    viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/2.5, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
else {
    viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
return YES;
}
于 2012-01-04T07:17:47.857 に答える
0

以下の方法を使用してこれを解決できます... UIDeviceOrientation に入らないでください.. use

UIInterfaceOrientation

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

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {

        viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/9, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
    else {
        viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
}

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

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/2.5, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
    else {
        viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
}
     -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
        {

            if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {

                viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/9, viewLogin.frame.size.width, viewLogin.frame.size.height);
            }
            else {
                viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
            }
            return YES;
        }
于 2012-01-04T07:24:02.487 に答える
0

UIKeyboardWillShowNotification に登録し、このコードを通知がスローされたときに呼び出されるメソッドに移動します。そのため、縦向きモードで textField を最初にクリックしてから横向きモードに変更すると、このメソッドが最初に呼び出されます。フレームを変更する前に、向きを確認することを忘れないでください (つまり、向きが横向きの場合にのみ行います)。

編集 -- コードの追加

あなたのビューではロードされました(または適切な場所で)、

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(keyBoardWillShow:) name:@"UIKeyboardWillShowNotification" object:nil];

メソッド keyBoardWillShow で:

-(void)keyBoardWillShow:(NSNotification *)noti {
     UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

        viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/9, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
    else {
        viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
    }
}
于 2012-01-04T07:27:16.837 に答える