///////////// Keyboard //////////////////////
-(void) keyboardDidShow: (NSNotification *)notif {
NSLog(@"Keyboard is visible");
// If keyboard is visible, return
if (keyboardVisible) {
NSLog(@"Keyboard is already visible. Ignore notification.");
return;
}
// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Save the current location so we can restore
// when keyboard is dismissed
offset = scrollview.contentOffset;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = scrollview.frame;
viewFrame.size.height -= keyboardSize.height;
scrollview.frame = viewFrame;
CGRect textFieldRect = [activeField frame];
textFieldRect.origin.y += 10;
[scrollview scrollRectToVisible:textFieldRect animated:YES];
NSLog(@"ao fim");
// Keyboard is now visible
keyboardVisible = YES;
}
-(void) keyboardDidHide: (NSNotification *)notif {
// Is the keyboard already shown
if (!keyboardVisible) {
NSLog(@"Keyboard is already hidden. Ignore notification.");
return;
}
// Reset the frame scroll view to its original value
scrollview.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);
// Reset the scrollview to previous location
scrollview.contentOffset = offset;
// Keyboard is no longer visible
keyboardVisible = NO;
}
-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField;
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"Registering for keyboard events");
// Register for the events
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector (keyboardDidShow:)
name: UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector (keyboardDidHide:)
name: UIKeyboardDidHideNotification
object:nil];
// Setup content size
scrollview.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH,
SCROLLVIEW_CONTENT_HEIGHT);
//Initially the keyboard is hidden
keyboardVisible = NO;
}
-(void) viewWillDisappear:(BOOL)animated {
NSLog (@"Unregister for keyboard events");
[[NSNotificationCenter defaultCenter]
removeObserver:self];
}
//////////////////////////////////////////////
IBOutlet UIScrollView *scrollview;
BOOL keyboardVisible;
CGPoint offset;
UITextField *activeField;
@property(nonatomic,retain) IBOutlet UIScrollView *scrollview;
and
@synthesis scrollview
#define SCROLLVIEW_CONTENT_HEIGHT 416
#define SCROLLVIEW_CONTENT_WIDTH 320