iosdev
iosdev

Reputation: 277

Keyboard orientation in UITextView in UIScrollView

In my application I am using a UIScrollView having UITextField and UITextView, I did the coding for the keyboard orientation in UITextField. It's working properly, but it's not working for UITextView. While editing in UITextView, the keyboard is fully hiding the UITextView. Here is my code

- (void)textFieldDidBeginEditing:(UITextField *)textField {
  [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
  CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
  CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
  CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
  CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
  CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
  CGFloat heightFraction = numerator / denominator;
  if (heightFraction < 0.0) {
    heightFraction = 0.0;
  } else if (heightFraction > 1.0) {
    heightFraction = 1.0;
  }
  UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
  if (orientation == UIInterfaceOrientationPortrait ||
    orientation == UIInterfaceOrientationPortraitUpsideDown) {
    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
  } else {
    animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
  }
  CGRect viewFrame = self.view.frame;
  viewFrame.origin.y -= animatedDistance;
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
  [self.view setFrame:viewFrame];
  [UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
  CGRect viewFrame = self.view.frame;
  viewFrame.origin.y += animatedDistance;
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
  [self.view setFrame:viewFrame];
  [UIView commitAnimations];    
}

Can any one help me to clear this?

Upvotes: 0

Views: 340

Answers (3)

Vineesh TP
Vineesh TP

Reputation: 7963

First add in your '.h' file

in did load

textView.delegate=self;

then given code for textView

textView.userinteractionEnabled=YES;

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20541

use delegate method of textview for UITextview like bellow

-(void)textViewDidBeginEditing:(UITextView *)textView;

and also

-(void)textViewDidEndEditing:(UITextView *)textView;

same like textField delegate method

For Example ...

-(void)textViewDidBeginEditing:(UITextView *)textView{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    self.view.frame=CGRectMake(0, -100, 320, 460);//just for an example
    [UIView commitAnimations];

}

-(void)textViewDidEndEditing:(UITextView *)textView{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    self.view.frame=CGRectMake(0,0, 320, 460);
    [UIView commitAnimations];
}

hope this help you..

:)

Upvotes: 1

prashant
prashant

Reputation: 1920

You can try the highly recommended "TPKeyboardAvoidingScrollView", available from: TPKeyboardAvoiding

Upvotes: 0

Related Questions