1
votes

Actually I have textfield in my sixth section of the tableview and a textview in the eleventh section of the textview.My problem is when I click on to the texfield of the sixth section the keyboard covers the text field and the same thing was happening with the textview.for that I used NSNotificationCenters. and I wrote the code as:

-(void)viewWillAppear:(BOOL)animated{

tableView.tableFooterView=nil;
[self reloadView];  
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification object:self.view.window]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                             name:UIKeyboardWillHideNotification object:self.view.window];
[tableView reloadData]; 

}

-(void)setViewMovedUp:(BOOL)movedUp{

[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; // if you want to slide up the view CGRect rect = mview.frame; if (movedUp) {
rect.origin.y = rect.origin.y - kOFFSET_FOR_KEYBOARD; rect.size.height = rect.size.height + kOFFSET_FOR_KEYBOARD; } else { rect.origin.y = rect.origin.y + kOFFSET_FOR_KEYBOARD; rect.size.height = rect.size.height - kOFFSET_FOR_KEYBOARD; } mview.frame = rect;

[UIView commitAnimations];

}

  • (void)keyboardWillShow:(NSNotification *)notif { // printf("\n Keyboard Will SHOW"); //keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately

    if ([notesTextView isFirstResponder] && mview.frame.origin.y >= 0) {
    [self setViewMovedUp:YES]; } else if (![notesTextView isFirstResponder] && mview.frame.origin.y < 0) { [self setViewMovedUp:NO]; }

    else if ([wineryTextField isFirstResponder] && mview.frame.origin.y >= 0) {
    [self setViewMovedUp:YES]; } else if (![wineryTextField isFirstResponder] && mview.frame.origin.y < 0) { [self setViewMovedUp:NO]; }

}

  • (void)keyboardWillHide:(NSNotification *)notif { // printf("\n Keyboard Will hide"); //keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately

    if ([notesTextView isFirstResponder] && mview.frame.origin.y >= 0) { [self setViewMovedUp:YES]; } else if (![notesTextView isFirstResponder] && mview.frame.origin.y < 0) { [self setViewMovedUp:NO]; }

    else if ([wineryTextField isFirstResponder] && mview.frame.origin.y >= 0) { [self setViewMovedUp:YES]; } else if (![wineryTextField isFirstResponder] && mview.frame.origin.y < 0) { [self setViewMovedUp:NO]; }

}

  • (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ( [ text isEqualToString: @"\n" ] ) {

    //event.eventNotes = notesTextView.text;
    
     if(textView==notesTextView)
    {
    
        //[self setViewMovedUp:NO];
        [textView resignFirstResponder];
    }
    return NO;
    

    }

    return YES;

}

  • (BOOL)textFieldShouldReturn:(UITextField *)textField {

    if(textField == wineryTextField) { [self setViewMovedUp:NO]; [wineryTextField resignFirstResponder]; } }

The above is the code I had written for Scrolling the view.but the problem is some times when I click on to the textfield it scroll down instead of scrolling up.The same was happed with the textView.the problem was getting when I navigate to another controller and come back and click on to eighter texfield or textview.

Pls any give a perfect solution for my problem.

Thanks all of u Guys.....

1

1 Answers

0
votes

You may want to consider using the following UITableView method:

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath
                    animated:(BOOL)animated
              scrollPosition:(UITableViewScrollPosition)scrollPosition

Where the indexPath contains the row you want shown at the top and scrollPosition is UITableViewScrollPositionTop.

If you want the tableview to scroll back, save the row that was at the top of the visible screen using the following UITableView method.

- (NSArray *)indexPathsForVisibleRows

And then with the first method mentioned above, scroll to the saved row.