lunedì 10 agosto 2015

Far scendere la tastiera per poter scrivere in un campo testo in un app iOS

Come si può far scendere o ripristinare la tastiera in uso in un campo testo di un'applicazione per iOS/Iphone?
Semplice, con questi due metodi è possibile ripristinarla al punto iniziale cliccando su di un qualsiasi punto dello schermo.
Con Xcode collegare il metodo a "Editing Did End" del campo testo.

- (void)textViewDidEndEditing:(UITextView *)textView
{
    CGAffineTransform translateUp = CGAffineTransformMakeTranslation(0.0, 0.0);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    self.view.transform = translateUp;
    [UIView commitAnimations];
}

//fieldName: UITextField defined in File.h
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if ([fieldName isFirstResponder] && [touch view] != fieldName) {
        [fieldName resignFirstResponder];
    }
    else if ([fieldCountry isFirstResponder] && [touch view] != fieldCountry) {
        [fieldCountry resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}