CyReX
CyReX

Reputation: 1576

Animating view when UITextField tapped

I would like to slide-up current view when UITextField has tapped cause of keyboard overlaps over textfield.

In same view-controller , It's too easy to handle it but I am using 2 view controller, 1st has header and footer part, 2nd has tableView and some custom cell (like comment field). When tapping comment field , Main View Controller should be slide up ( which has header and footer part ).

I am using in MainViewcontroller ( which should be slide-up)

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    const int movementDistance = 160; // 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.wizardQuestionView.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

In subViewController ( MainViewController's tableView's custom cell view ) In header file , I added UITextFieldDelegate

and .m file

- (void)textFieldDidBeginEditing:(UITextField *)textField
{


    if(textField.tag == 1)
    {
        NSLog(@"Comment View");

        MainViewController *mc = [[MainViewController alloc] init];

        //It's going Main ViewController 's method
        [mc animateTextField: textField up: YES];
    }

}

Any help would be appreciate .

Regards

Onder

Upvotes: 0

Views: 103

Answers (1)

Tiago Almeida
Tiago Almeida

Reputation: 14237

Your MainViewController needs to be the delegate of your SubViewController.

Then, when the textField is pressed you need to comunicate that to your delegate. Like this:

if([self.delegate respondsToSelector(@selector(textDidBeginEditing:)])
    [self.delegate textDidBeginEditing:textField];

For this you need to define a protocol:

@protocol SubViewControllerProtocol<NSObject>
    -(void) textDidBeginEditing:(UITextField*) textField;
@end

and in your SubViewController you need to create the delegate:

@property (nonatomic, weak) id<SubViewControllerProtocol> delegate;

Then in your MainViewController you need to set the delegate:

self.subViewController.delegate = self;

Then, in your MainViewController implement the textDidBeginEditing:

-(void) textDidBeginEditing:(UITextField*) textField {
     [self animateTextField: textField up: YES]
}

Upvotes: 1

Related Questions