I am not able to sort of where is the mistake in my code . When i click the "Submit" button then the keyboard rises again. Here i am using the NSNotificatioCenter
which scrolls the view according to keyboard height.
Have a look on code below.
-(void)viewDidLoad
{
[super viewDidLoad];
[[self navigationController] setNavigationBarHidden:YES];
// 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);
keyboardVisible = NO;
}
-(void) viewWillDisappear:(BOOL)animated
{
NSLog (@"Unregister for keyboard events");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(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:UIKeyboardFrameBeginUserInfoKey];
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 = [activeView 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) textViewShouldBeginEditing:(UITextView*)textView {
activeView = textView;
return YES;
}
-(void)textViewDidEndEditing:(UITextView *)textView{
if(textView == _textViewFeedback)
[_textViewFeedback resignFirstResponder];
else
[_textViewEmail resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
if([text isEqualToString:@"\n"]){
[textView resignFirstResponder];
}
return YES;
}
- (IBAction)BtnActionSubmitFeedback:(id)sender {
_textViewEmail.text = nil;
_textViewFeedback.text = nil;
NSString *errorMessage = [self validateForm];
if (errorMessage) {
showAlert(@"Warning", errorMessage, nil, nil, @"Dismiss");
return;
}
}
Add a delegate of UITextField
Now set your
textField.delegate = self;
inviewDidLoad
Add this delegate method to dismiss Keyboard
OR
You may Put
[self.view endEditing:YES];
in your Submit button action to dismiss Keyboard.