0
votes

I have quite a long form in ipad having uitextfields and uitextviews. I am able to scroll up when the keyboard hides the uitextfield using keyboard notifications. but when the uitextview becomes active and it is under the keyboard it just slides enough so that i can see the blinking cursor. Is this the normal behavior? If not how can i scroll the entire uitextview upwards so that the entire uitextview is visible at the time of editing?

Here is the code..

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name: UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark Keyboard Events

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return NO;
}

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-(void)keyboardWasShown:(NSNotification *)aNotification
{

if (displayKeyboard==YES) {
    return;
}
NSDictionary* info = [aNotification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
//NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;


offset = _scrollView.contentOffset;

CGRect viewFrame = _scrollView.frame;

viewFrame.size.height -= keyboardSize.height-tabbarHt;
_scrollView.frame = viewFrame;

CGRect textFieldRect = [activeField frame];
textFieldRect.origin.y += 10;
[_scrollView scrollRectToVisible: textFieldRect animated:YES];
displayKeyboard = YES;

}

-(void)keyboardWillBeHidden:(NSNotification *)aNotification
{

if (!displayKeyboard) {
    return; 
}

_scrollView.frame = CGRectMake(0, 0, 1024, 655);
_scrollView.contentOffset =offset;
displayKeyboard = NO;
}

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField;
return YES;
}
1
you have to set the method of up the scroll in UITextViewDelegate methodHiren
@CocoaMatters Can you tell me how to go about doing that?southpark
how you doing for UITextField?Hiren
@CocoaMatters using keyboard didshow and didhide notifications..and calling the corressponding methods on these notifications and in those methods adjusting the scroll view size and if my textfield is hidden I scroll it upwards. The same code partially works for the textview. it scrolls up but not completely. Just one line of textview is visible.southpark
can you post this code ?Hiren

1 Answers

0
votes

Give delegate to your UITextView

CGRect activeField;

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    activeField = textView.frame;
}

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
   activeField = textField.frame;
   return YES;
}

Try With this code. Every time you have given only textField rect size. when you call text view you have to pass textview frame size