0

我有一个子视图中的两个文本框作为viewLogin。当尝试进入文字键盘时隐藏它们。我通过移动viewLogin解决了这个问题。 代码是...方向更改时,键盘会隐藏textField吗?

这里
- (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); 
    } 
} 

问题,我面对的是,如果我第一次更改方向为横向,然后单击文本框其工作正常,但如果我第一次在纵向模式下点击文本框,然后改为横向左/右它不是事先

回答

1

这就是苹果应用(看看的KeyboardAccessory example):杉木ST,在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; 
        }]; 
} 

我使用它,我对方向变化没有任何问题。

0

工作..

能有所帮助我解决这个....

感谢,因为你的textFieldDidBeginEditing方法被称为后在第一种情况下和CA之前第二种情况下, LLED所以你必须在你的文本框的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; 
} 
0

你可以用下面的方法来解决这个的代码......不要误会成UIDeviceOrientation ..使用

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; 
     } 
+0

是的,我试过这个,即使它不工作... – 2012-01-04 08:54:29

0

注册到UIKeyboardWillShowNotification,并将此代码移动到在引发通知时调用的方法。因此,如果您首先在纵向模式中单击textField,然后将其更改为横向,则首先调用此方法。 不要忘记检查方向更改帧之前(即做到这一点只有当方向是横向)

edit--添加代码

在你看来没有负载(或者其它任何合适的),

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); 
    } 
} 
+0

你可以提供基本的代码这样做... – 2012-01-04 09:00:56

+0

已编辑与代码的答案。键盘隐藏时,您应该重置文本字段的位置。 – 2012-01-04 09:24:17