In my iphone app. in MFMailComposerView view when i am clicking to recepients then key board appears . After i am clicking return key in the key board. but key board not disappear.
Use UIWindow notifications keyboard and just use bellow code for display the MFMailComposerViewController ..
- (IBAction)showMailController {
//Present mail controller on press of a button, set up notification of keyboard showing and hiding
[nc addObserver:self selector:#selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:#selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
//... and so on
}
- (void)keyboardWillShow:(NSNotification *)note {
//Get view that's controlling the keyboard
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
//set up dimensions of dismiss keyboard button and animate it into view, parameters are based on landscape orientation, the keyboard's dimensions and this button's specific dimensions
CGRect t;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
button.frame = CGRectMake(324,(290-t.size.height),156,37);
button.alpha = 0.0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[[[[[firstResponder superview] superview] superview] superview] addSubview:button];
button.alpha = 1.0;
button.frame = CGRectMake(324,(253-t.size.height),156,37);
[UIView commitAnimations];
}
- (IBAction)dismissKeyboardInMailView {
//this is what gets called when the dismiss keyboard button is pressed
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
[firstResponder resignFirstResponder];
}
- (void)keyboardWillHide:(NSNotification *)note {
//hide button here
[button removeFromSuperview];
}
i got some of this code from this link..
programmatically-align-a-toolbar-on-top-of-the-iphone-keyboard
You can use the following code for this.
UIWindow *mainWin = [[UIApplication sharedApplication] keyWindow];
UIView *responder = [mainWin performSelector:#selector(firstResponder)];
[responder resignFirstResponder];
But if you use this in your app, Apple will surely reject your app because UIWindow's firstResponder method is a private API.
Reference : SO
Related
I am trying to dismiss keyboard on tap anywhere in the view. Here is my code
- (void)registerForNotifcationOfKeyboard
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = activeField.superview.frame;
bkgndRect.size.height += kbSize.height;
[activeField.superview setFrame:bkgndRect];
[scrollView setContentOffset:CGPointMake(0.0,kbSize.height/2 - activeField.frame.origin.y) animated:YES];
}
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void) textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
-(BOOL) disablesAutomaticKeyboardDismissal
{
return NO;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)viewDidLoad
{
[self registerForNotifcationOfKeyboard];
self.progressBar.hidden = YES;
UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[super viewDidLoad];
}
-(void) dismissKeyboard
{
[activeField resignFirstResponder];
}
when I press tap anywhere this function dismissKeyboard does get called but it never dismiss the keyboard.
Kindly If anyone has any idea
Best Regards
This always works for me:
//if `dismissKeyboard` is located in your `UIViewController`'s sublass:
-(void) dismissKeyboard
{
[self.view endEditing:YES];
}
From UIView Class Reference:
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder.
If it finds one, it asks that text field to resign as first responder. If the force parameter is set to **YES**, the text field is never even asked; it is **forced to resign**.
I complelety agree with #Lukasz : Use view's endEditing property to dissmiss keyboard
-(void) dismissKeyboard
{
[self.view endEditing:YES]; //this will dissmiss keyboard;
}
Try the below code.
before that use UITextFieldDelegate in .h file.
in .m file
textfieldname.delegate=self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textfieldname resignFirstResponder];
return YES;
}
The above code will dismiss keyboard on pressing return key.use below code inside your function to dismiss the keyboard.
[textfieldname resignFirstResponder];
if you want to dismiss your keyboard by pressing anywhere inside your view then there are 2 things you can do:
1:- Have a UIButton which has [UIColor clearColor] and which is equal to the size of the view and in the IBAction of the button you dismiss your keyboard. This is not a good practice though it seems to work.
2:- Go to identity inspector and change your view's class to UIControl class and then add an IBAction to your class that dismisses your keyboard.
I hope this helps.
Cheers!!
here is problem, when i debug my program, i found keyboardWillShow function not responds every time. just first time, it will be called by program. here is my code, i dont know whats wrong in my code, but, when the keyboard first appeared, the function run well.
- (void)keyboardWillShow:(NSNotification *)notification {
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.textview.frame;
newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
textview.frame = newTextViewFrame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
/*
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of the keyboard.
*/
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
// textview.frame = self.view.bounds;
[self save];
[UIView commitAnimations];
}
and i regist notification
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
and remove it in here
- (void)viewDidUnload
{
[super viewDidUnload];
[self save];
self.textview = nil;
self.title = nil;
self.tags = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
i resign firstresponder, here is my code
- (IBAction)save:(id)sender {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addNote)] autorelease];
[textview resignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(save:)] autorelease];
[self setUpUndoManager];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[self save];
return YES;
}
Make sure that you are not writing any code to remove observer.....
Please provide keyboardWillHide method also....
is there something that can call a -(void) every time a view shows.
I am rotating between two views, so when i go to the second view and then return to the first view I want a void to be called automatically from the first view
I've try to do this with -(void)viewWillAppear:(BOOL)animated and - (void)viewDidLoad
by putting a NSLog in them but it doesn't print when i return to the first view.
any suggestions?
If you are updating the first view with data from the second, use an observer on the model that is being updated. If the second view is being removed as a subview, you can observe the subviews. Alternatively, you can use a block callback closure.
In the interface:
IBAction(^doOnClose)(void);
and:
#property (nonatomic, copy) IBAction(^doOnClose)(void);
In the method that closes your second view:
if(doOnClose) doOnClose();
And finally to define it from your first view:
view2.doOnClose = ^{/*Do this when view 2 closes*/};
I have two methods in the app delegate
-(void)goTo1{
[self.window addSubview:[viewController view]];
[settingsViewController.view removeFromSuperview];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
//removing the settingsViewController form the view and setting the animation
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache:NO];
[UIView commitAnimations];
[settingsViewController release];
settingsViewController = nil; }
and
-(void)goTo2{
//calling the .xib file and the SettingsViewController
SettingsViewController *aSettingsView = [[SettingsViewController alloc] initWithNibName:#"Settings" bundle:nil];
[self setSettingsViewController:aSettingsView];
[aSettingsView release];
[self.window addSubview:[settingsViewController view]];
//moving the view 30px down
[[settingsViewController view] setFrame:CGRectMake(0, 20, 320, 460)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
//setting the animation
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
[UIView commitAnimations];
[webskiAppBetaViewController release];
webskiAppBetaViewController = nil;
}
and IBActions in the 2 view controllers
first One
-(IBAction)goToView2{
WebskiAppBetaAppDelegate *mainDelegate = (WebskiAppBetaAppDelegate *)[[UIApplication sharedApplication] delegate];
[mainDelegate goTo2];
}
and second one
-(IBAction)goToView1{
WebskiAppBetaAppDelegate *maniDelegate = (WebskiAppBetaAppDelegate *)[[UIApplication sharedApplication] delegate];
[maniDelegate goTo1];
}
so now when i call goToVIew1 I also want to run a method that is in the view 1
something like this
-(IBAction)goToView1{
WebskiAppBetaAppDelegate *maniDelegate = (WebskiAppBetaAppDelegate *)[[UIApplication sharedApplication] delegate];
[maniDelegate goTo1];
[self methodInFirstVIew]; //this method is in the first view
}
I must be missing something fundamental here. I have a UITableView inside of a NavigationViewController. When a table row is selected in the UITableView (using tableView:didSelectRowAtIndexPath:) I call pushViewController to display a different view controller. The new view controller appears correctly, but when I pop that view controller and return the UITableView is resized as if the keyboard was being displayed. I need to find a way to have the keyboard hide before I push the view controller so that the frame is restored correctly. If I comment out the code to push the view controller then the keyboard hides correctly and the frame resizes correctly.
The code I use to show the keyboard is as follows:
- (void) keyboardDidShowNotification:(NSNotification *)inNotification {
NSLog(#"Keyboard Show");
if (keyboardVisible) return;
// We now resize the view accordingly to accomodate the keyboard being visible
keyboardVisible = YES;
CGRect bounds = [[[inNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
bounds = [self.view convertRect:bounds fromView:nil];
CGRect tableFrame = tableViewNewEntry.frame;
tableFrame.size.height -= bounds.size.height; // subtract the keyboard height
if (self.tabBarController != nil) {
tableFrame.size.height += 48; // add the tab bar height
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(shrinkDidEnd:finished:contextInfo:)];
tableViewNewEntry.frame = tableFrame;
[UIView commitAnimations];
}
The keyboard is hidden using:
- (void) keyboardWillHideNotification:(NSNotification *)inNotification {
if (!keyboardVisible) return;
NSLog(#"Keyboard Hide");
keyboardVisible = FALSE;
CGRect bounds = [[[inNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
bounds = [self.view convertRect:bounds fromView:nil];
CGRect tableFrame = tableViewNewEntry.frame;
tableFrame.size.height += bounds.size.height; // add the keyboard height
if (self.tabBarController != nil) {
tableFrame.size.height -= 48; // subtract the tab bar height
}
tableViewNewEntry.frame = tableFrame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(_shrinkDidEnd:finished:contextInfo:)];
tableViewNewEntry.frame = tableFrame;
[UIView commitAnimations];
[tableViewNewEntry scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
NSLog(#"Keyboard Hide Finished");
}
I trigger the keyboard being hidden by resigning first responser for any control that is the first responder in ViewWillDisappear. I have added NSLog statements and see things happening in the log file as follows:
Show Keyboard
ViewWillDisappear: Hiding Keyboard
Hide Keyboard
Keyboard Hide Finished
PushViewController (an NSLog entry at the point I push the new view controller)
From this trace, I can see things happening in the right order, but It seems like when the view controller is pushed that the keyboard hide code does not execute properly.
Any ideas would be really appreciated. I have been banging my head against the keyboard for a while trying to find out what I am doing wrong.
-- Added didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0: // Required Info
// removed to simplify
case 1: // Optional Info
switch (indexPath.row) {
case 0:
[self showTextDetailPicker: #"Enter a description"
tag: tDescriptionPicker
sourceTarget: self.newRecord
fieldSource: #selector(description)];
break;
default:
break;
}
break;
default:
break;
}
}
- (void) showTextDetailPicker: (NSString*) titleText tag:(int)tagID sourceTarget:(NSObject*)target fieldSource:(SEL)selector{
FieldEditorViewController *fe = [[FieldEditorViewController alloc] init];
fe.titleText = titleText;
fe.fieldText = [target performSelector: selector];
fe.tag = tagID;
// Replace default back button with one that just says 'Back'
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIButtonTypeInfoLight
target:nil action:nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
[fe setDelegate: self];
[self.navigationController pushViewController:fe animated:YES];
[fe release];
}
Right before you push your view controller, find the first responder and call resign. Use the category from this SO post to see how to find the first responder recursively (not sure if you're already doing this).
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *fp = [[self view] findFirstResponder];
[fp resignFirstResponder];
// Push new view controller here.
NextViewController *controller = [[NextViewController alloc] init];
[[self navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
}
The other thing to keep in mind is that your table view is getting resized automatically because your root view controller is derived from UITableViewController (or so it seems). If you make your root view controller a regular UIViewController that contains a UITableView, you can manipulate the frame of the table view manually more easily--at least that's been my experience.
How do I dismiss the keyboard in the MFMailComposeViewController when the user hits the return key?
There is no way to change the behavior of an MFMailComposeViewController (edit: by that I mean using only public APIs in such a way that your app won't be rejected by Apple -- there is very probably a way to do this "illegally" if you're building an in-house app).
http://developer.apple.com/iphone/library/documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html
Quote: "Important: The mail composition interface itself is not customizable and must not be modified by your application."
It's in line with Apple's HIG to always provide a single, consistent behavior for sending mail.
I believe the following solution I've just developed does not modify or damage the reliable user experience in the mail view controller—it merely enhances it for those who want to dismiss the keyboard. I do not change the functionality of existing elements, I only add a new element. The part of the code that gets the height of the keyboard after it is shown comes from this answer. Here goes:
- (IBAction)showMailController {
//Present mail controller on press of a button, set up notification of keyboard showing and hiding
[nc addObserver:self selector:#selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:#selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
//... and so on
}
- (void)keyboardWillShow:(NSNotification *)note {
//Get view that's controlling the keyboard
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
//set up dimensions of dismiss keyboard button and animate it into view, parameters are based on landscape orientation, the keyboard's dimensions and this button's specific dimensions
CGRect t;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
button.frame = CGRectMake(324,(290-t.size.height),156,37);
button.alpha = 0.0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[[[[[firstResponder superview] superview] superview] superview] addSubview:button];
button.alpha = 1.0;
button.frame = CGRectMake(324,(253-t.size.height),156,37);
[UIView commitAnimations];
}
- (IBAction)dismissKeyboardInMailView {
//this is what gets called when the dismiss keyboard button is pressed
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
[firstResponder resignFirstResponder];
}
- (void)keyboardWillHide:(NSNotification *)note {
//hide button here
[button removeFromSuperview];
}