2012-01-09 87 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/