Attaching a UIToolbar to the iPhone’s Keyboard – Extended
- (void)keyboardWillShow:(NSNotification *)notification {
double animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardBounds = [(NSValue *)[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
[UIView beginAnimations:@"showKeyboardAnimation" context:nil];
[UIView setAnimationCurve:animationCurve];
[UIView setAnimationDuration:animationDuration];
self.view.frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height - keyboardBounds.size.height);
CGRect textFieldFrame = _textField.frame;
textFieldFrame.size.width = keyboardBounds.size.width - 36.0 - 50.0; // Done button is 50 pixels wide, 3 times 12 pixels space
_textField.frame = textFieldFrame;
_toolbar.items = [NSArray arrayWithObjects:_textFieldBarButtonItem,_flexibleSpaceBarButtonItem,_doneBarButtonItem,nil];[UIView commitAnimations];
}- (void)keyboardWillHide:(NSNotification *)notification {
double animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardBounds = [(NSValue *)[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
[UIView beginAnimations:@"hideKeyboardAnimation" context:nil];
[UIView setAnimationCurve:animationCurve];
[UIView setAnimationDuration:animationDuration];
self.view.frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height + keyboardBounds.size.height);CGRect textFieldFrame = _textField.frame;
textFieldFrame.size.width = keyboardBounds.size.width - 24.0; // 12 pixels space to either side
_textField.frame = textFieldFrame;
_toolbar.items = [NSArray arrayWithObject:_textFieldBarButtonItem];
[UIView commitAnimations];
}
浙公网安备 33010602011771号