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