2012-01-09 40 views
0

在我的iPad應用程序中,我有幾個textViewtextField's。當我點擊textField時,鍵盤覆蓋了textField。所以我正在實現下面的代碼來移動textview。但輪到portraitUpsideDown它不能正常工作。它向相反的方向滑動屏幕。那麼我該如何解決這個問題?鍵盤隱藏在ipad中的不同方向的textfield

-(void) animateTextField: (UITextView *) textField up: (BOOL) up 
{ 
    int txtPosition = (textField.frame.origin.y - 540); 
    const int movementDistance = (txtPosition < 0 ? 0 : txtPosition); // tweak as needed 
    const float movementDuration = 0.3f; // tweak as needed 

    int movement = (up ? -movementDistance : movementDistance); 

    [UIView beginAnimations: @"anim" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 
    self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
    [UIView commitAnimations]; 
} 

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

-(void)textViewDidEndEditing:(UITextView *)textField 
{ 
    [self animateTextField: textField up: NO]; 
} 

-(BOOL)textFieldShouldReturn:(UITextView *)theTextField 
{ 
    [theTextField resignFirstResponder]; 
    return YES; 
} 
+0

oopss解釋了這個....剛纔我檢查了你的問題again..Its爲iPad ...所有的方位都接受......對不起,給了你錯誤的信息... – 2012-01-09 05:53:27

+0

@ DineshRaja.MaG那麼我該如何解決這個問題? – crazy2431 2012-01-09 06:00:05

回答

0

如果你的方法是這樣的。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

試試這個。我不完全清楚。但我想幫助你。可能是xy座標不能在任何方向上改變。所以試試這個。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
    if(interfaceOrientation=UIInterfaceOrienationPotraitUpsideDown){ 
      //Declare txtPos globally... 
      txtPos=(textField.frame.origin.y + 540); 
     } 
    if(interfaceOrientation=UIInterfaceOrienationPotrait) 
     { 
      txtPos=(textField.frame.origin.y - 540); 
     } 
    return(YES); 
    } 

in animate method。 指定textPostxtPosition變量..

+0

雅與它相同的上述..但在旋轉portraitUpsidedDown其不工作,而不是向上移動向下。它只適用於縱向視圖不適用於portraitUpSideDown。 – crazy2431 2012-01-09 05:55:44

+0

除了你的鍵盤,其他的一切都在改變其方向upsideDown? – 2012-01-09 06:02:17

+0

雅everyth其他正在改變顛倒,而不是移動屏幕上的向上向下移動和隱藏textview..its做扭轉它在縱向 – crazy2431 2012-01-09 06:07:14

0

瘋狂,

只需添加另一種功能:

- (void) animateTextView: (UITextView*) textView up: (BOOL) up 
{ 
    const int movementDistance = 80; // tweak as needed 
    const float movementDuration = 0.3f; // tweak as needed 

    int movement = (up ? -movementDistance : movementDistance); 

    [UIView beginAnimations: @"anim" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 
    self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
    [UIView commitAnimations]; 
} 

然後調用它像:

- (void)textViewDidBeginEditing:(UITextView *)textView { 
    [self animateTextView: textView up: YES]; 
} 

- (void)textViewDidEndEditing:(UITextView *)textView { 
    [self animateTextView: textView up: NO]; 
} 
0

您應該使用鍵盤將顯示並將隱藏通知以捕獲鍵盤事件並相應地調整您的視圖。

- (void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

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

} 

- (void)keyboardWillShow:(NSNotification *)notification { 
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame); 
    CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; 
    UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; 
    UIViewAnimationOptions animationOption = animationCurve << 16; 

    [UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{ 
     // adjust height using keyboardHeight 
    } completion:^(BOOL finished) { 

    }]; 
} 

- (void)keyboardWillHide:(NSNotification *)notification { 
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame); 
    CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; 
    UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; 
    UIViewAnimationOptions animationOption = animationCurve << 16; 

    [UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{ 
     // adjust height using keyboardHeight 
    } completion:^(BOOL finished) { 

    }]; 
} 

本博客文章中詳細

http://charlie.cu.cc/2015/10/solution-to-the-ios-software-keyboard-cover-part-of-the-ui/