0

我在子视图中有两个文本字段作为 viewLogin。当尝试输入文本时,键盘隐藏了它们。我通过移动 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 方法在第一种情况下稍后调用,而在第二种情况下在调用之前调用,因此您必须在 textfield 编辑的 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 .. 使用

UI界面方向

- (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 回答