C'est une solution incomplète, mais il devrait vous donner un bon point de départ.
Ajouter le suivant ivars à votre UIViewController:
CGRect keyboardSuperFrame; // frame of keyboard when initially displayed
UIView * keyboardSuperView; // reference to keyboard view
Ajouter un inputAccessoryView à votre texte contrôleur. J'ai créé une petite vue pour insérer le accessoryView:
accView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
accView.backgroundColor = [UIColor clearColor];
textField.inputAccessoryView = accView;
J'ai ajouté le code ci-dessus à l' -(void)loadView
S'inscrire pour recevoir UIKeyboardDidShowNotification et UIKeyboardDidHideNotification lorsque la vue est chargé:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
return;
}
Ajouter des méthodes à précisé que les sélecteurs pour les notifications:
// method is called whenever the keyboard is about to be displayed
- (void)keyboardWillShow:(NSNotification *)notification
{
// makes keyboard view visible incase it was hidden
keyboardSuperView.hidden = NO;
return;
}
// method is called whenever the keyboard is displayed
- (void) keyboardDidShow:(NSNotification *)note
{
// save reference to keyboard so we can easily determine
// if it is currently displayed
keyboardSuperView = textField.inputAccessoryView.superview;
// save current frame of keyboard so we can reference the original position later
keyboardSuperFrame = textField.inputAccessoryView.superview.frame;
return;
}
Ajouter des méthodes à suivre touché et mise à jour du clavier vue:
// stops tracking touches to divider
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect newFrame;
CGRect bounds = [[UIScreen mainScreen] bounds];
newFrame = keyboardSuperFrame;
newFrame.origin.y = bounds.size.height;
if ((keyboardSuperView.superview))
if (keyboardSuperFrame.origin.y != keyboardSuperView.frame.origin.y)
[UIView animateWithDuration:0.2
animations:^{keyboardSuperView.frame = newFrame;}
completion:^(BOOL finished){
keyboardSuperView.hidden = YES;
keyboardSuperView.frame = keyboardSuperFrame;
[textField resignFirstResponder]; }];
return;
}
// updates divider view position based upon movement of touches
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch;
CGPoint point;
CGFloat updateY;
if ((touch = [touches anyObject]))
{
point = [touch locationInView:self.view];
if ((keyboardSuperView.superview))
{
updateY = keyboardSuperView.frame.origin.y;
if (point.y < keyboardSuperFrame.origin.y)
return;
if ((point.y > updateY) || (point.y < updateY))
updateY = point.y;
if (keyboardSuperView.frame.origin.y != updateY)
keyboardSuperView.frame = CGRectMake(keyboardSuperFrame.origin.x,
point.y,
keyboardSuperFrame.size.width,
keyboardSuperFrame.size.height);
};
};
return;
}
Avertissements:
- Lors de sa démission en tant que premier a répondu, le clavier revient à sa position d'origine avant de glisser hors de l'écran. Pour faire rejetant le clavier plus fluide, vous devez d'abord créer une animation pour déplacer le clavier hors de l'écran, puis masquer la vue. Je vais laisser cette partie comme un exercice pour les lecteurs.
- J'ai uniquement testé sur le simulateur iOS 5 et un iPhone avec iOS 5. Je n'ai pas testé avec les versions antérieures d'iOS.
Le SlidingKeyboard projet que j'ai créé pour tester ce concept est disponible à partir de GitHub dans les exemples répertoire de BindleKit:
https://github.com/bindle/BindleKit
Edit: la mise à Jour de l'exemple à l'adresse de premier avertissement.