UItextview在iPhone应用程序中上下移动(当键盘出现并消失时)

问题描述:

当键盘出现和消失时,如何在iphone应用程序中将UItextviewUIScrollView中上下移动。UItextview在iPhone应用程序中上下移动(当键盘出现并消失时)

+1

你最好做一些以前问;) – Kjuly

+0

@nila显示我的回答 –

+0

@nila代码做好准备。请参阅我的回答 – DharaParekh

只需添加通知观察员键盘的行动-viewDidLoad-viewWillAppear:

NSNotificationCenter * notificationCetner = [NSNotificationCenter defaultCenter]; 
[notificationCetner addObserver:self 
         selector:@selector(_keyboardWasShown:) 
          name:UIKeyboardDidShowNotification 
         object:nil]; 
[notificationCetner addObserver:self 
         selector:@selector(_keyboardWillHide:) 
          name:UIKeyboardWillHideNotification 
         object:nil]; 

然后在-_keyboardWasShown: & -_keyboardWillHide:方法更新文本视图的框架。

有一个很好的库叫TPKeyboardAvoiding,你可以使用(找到它here)。

或者,您可以自己编程并使用UIScrollViewcontentOffset属性向下或向上移动内容。然后您应该听取键盘的keyboardDidShowkeyboardWillHide方法。

希望它有帮助!

其中一个方法是--->在viewDidLoad中或viewWillAppear中添加这些观察员

NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 
[center addObserver:self selector:@selector(keyboardDisappeared) name:UIKeyboardWillHideNotification object:nil]; 
[center addObserver:self selector:@selector(keyboardAppeared) name:UIKeyboardWillShowNotification object:nil]; 

比写keyboardDisappeared和keyboardAppeared如下

-(void) keyboardDisappeared 
{ 
    [UIView beginAnimations:@"animate" context:nil]; 
    [UIView setAnimationDuration:0.2f]; 
    [UIView setAnimationBeginsFromCurrentState: NO]; 
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+100(same as you do in keyboardAppeared), self.view.frame.size.width, self.view.frame.size.height); 
    [UIView commitAnimations]; 
} 

-(void) keyboardAppeared 
{ 
    [UIView beginAnimations:@"animate" context:nil]; 
    [UIView setAnimationDuration:0.2f]; 
    [UIView setAnimationBeginsFromCurrentState: NO]; 
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-(as much you want), self.view.frame.size.width, self.view.frame.size.height); 
    [UIView commitAnimations]; 
} 

不要忘记删除观察者! !

+0

+1,以了解使用通知的情况,但您复制UIView动画代码的书的年龄是多少?我们已经有3年基于块的动画了! –

+0

@AshleyMills并不重要,因为代码永远不会变老。重要的是,它不会影响你的程序效率。 – Suryakant

+1

根据Apple的文档 - 'beginAnimations:context:'**在iOS 4.0及更高版本中不鼓励使用此方法。您应该使用基于块的动画方法来指定您的动画。** –

第一组的TextView的委托,然后尝试实现这些方法....

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView 
    { 
     [self scrollViewToCenterOfScreen:textView]; 
     return YES; 
    } 

    - (void) scrollViewToCenterOfScreen:(UIView *)theView 
    { 
     CGFloat viewCenterY = theView.center.y; 
     CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 

     CGFloat availableHeight = applicationFrame.size.height - 300;   // Remove area covered by keyboard 

     CGFloat y = viewCenterY - availableHeight/2.0; 

     if (y < 0) 
     { 
      y = 0; 
     } 
     [scrollView setContentOffset:CGPointMake(0, y) animated:YES]; 
    } 

// -------- Modified -------- 


    - (BOOL)textView:(UITextView *)txtView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)tempText 
{ 
    if([tempText rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location == NSNotFound) 
    { 
     return YES; 
    } 

    [txtView resignFirstResponder]; 
    [self.scrollView setContentOffset:CGPointMake(0.0f, 0.0f) animated:TRUE]; 
    return NO; 
} 
+1

你应该使用'UIKeyboardWillHide/ShowNotification',那么你将不需要硬编码大小。 –

+0

它的k,但是在这个代码中你遇到了什么问题,并且赞成这个答案... –

+0

@Dharmbir Choudhary:我可以将textview向上移动。但不能下移使用此代码 – Nila