keyboardWillShow not respond - iphone

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....

Related

How to call a method on click on textfiled rathe than text changes

I want to move the view when TextField is clicked but it works only on textDidChange when we change text or write anything so how to call method when we click the TextField.
-(void)textFieldTextDidChange:(UITextField*)tf{
[self showAnimationPests];
}
-(void) showAnimationPests{
[UIView animateWithDuration:0.5
animations:^{
self.view.frame = CGRectMake(0,-300,1024,768);
}];
}
instead of textchange i want on textfield click
In .h file
IBOutlet UIButton*button1;
IBOutlet UIButton*button2;
IBOutlet UIButton*button3;
IBOutlet UIButton*button4;
IBOutlet UIButton*button5;
IBOutlet UIButton*button6;
IBOutlet UIButton*button7;
IBOutlet UIButton*button8;
IBOutlet UIButton*button9;
IBOutlet UIButton*button10;
#property(nonatomic,retain)IBOutlet UIButton*button1;
#property(nonatomic,retain)IBOutlet UIButton*button2;
#property(nonatomic,retain)IBOutlet UIButton*button3;
#property(nonatomic,retain)IBOutlet UIButton*button4;
#property(nonatomic,retain)IBOutlet UIButton*button5;
#property(nonatomic,retain)IBOutlet UIButton*button6;
#property(nonatomic,retain)IBOutlet UIButton*button7;
#property(nonatomic,retain)IBOutlet UIButton*button8;
#property(nonatomic,retain)IBOutlet UIButton*button9;
#property(nonatomic,retain)IBOutlet UIButton*button10;
Use
- (void)textFieldDidBeginEditing:(UITextField *)textField
delegate method. It is called when UITextField becomes the first responder. The moment you click in your textfield this method gets called.
Add this Code :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(slideUpView:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(slideDownView:) name:UIKeyboardWillHideNotification object:nil];
-(void)slideDownView:(NSNotification*)notification
{
[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];
[UIView animateWithDuration:animationDuration
delay:0.0
options:animationCurve
animations:^{
self.view.frame = CGRectMake(0, 0, 320, 480);
}
completion:^(BOOL finished){
NSLog(#"Slide down Done..!");
}];
}
-(void)slideUpView:(NSNotification*)notification
{
[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];
//
[UIView animateWithDuration:animationDuration
delay:0.0
options:animationCurve
animations:^{
self.view.frame = CGRectMake(0, -keyboardFrame.size.height + 70, 320, 416);
}
completion:^(BOOL finished){
NSLog(#"Slide up Done..!");
}];
}
You can either do it by using delegate method
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self showAnimationPests];
}
Or add tap gasture to your textField
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap)];
[singleTap setNumberOfTapsRequired:1];
[tf addGestureRecognizer:singleTap];
}
- (void)handleSingleTap
{
[self showAnimationPests];
}
Firstofall add <UITextFieldDelegate> in your .h file than
in .m file
- (void)viewDidLoad
{
yourtextfield.delegate = self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == yourtextfield)
{
[self showAnimationPests];
}
}
-(void) showAnimationPests
{
[UIView animateWithDuration:0.5
animations:^{
self.view.frame = CGRectMake(0,-300,1024,768);
}];
}

how to dismiss the key board in mfmailcomposerview

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

Not been able to dismiss keyboard on tap

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!!

Ensure a view remains full screen upon rotate

I have a simple UIWebView that I've added to my UIViewController in the viewDidLoad method:
CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.webView = [[UIWebView alloc] initWithFrame:rect];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.webView];
And it looks great, but when I rotate the phone, the width and height remain the same so now it's too wide for the update view frame. I also tried using self.view.bounds, but it didn't make any difference.
So how do you ensure that a view that is fullscreen on load, stays the same size when rotated? (without using IB)
What you are doing is correct & should work in most scenarios. But since I have no idea about your View Stack. I will suggest a sure shot way -
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
CGRect rect;
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
else
{
//some other dimensions.
}
self.webView = [[UIWebView alloc] initWithFrame:rect];
}
Because web view not called only once it need to be called again
to set the new frame
self.webView = [[UIWebView alloc] initWithFrame:rect];
so you have to register notification in viwewillappear or viewdidload
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(viewBecamePortrait:) name:#"orientationIsPortrait" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(viewBecameLandscape:) name:#"orientationIsLandscape" object:nil];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
NSNotification* notification = [NSNotification notificationWithName:#"orientationIsPortrait" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}else {
NSNotification* notification = [NSNotification notificationWithName:#"orientationIsLandscape" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
}
Then implement
-(void)viewBecameLandscape:(id)sender{
if(webview){
[webview.setframe(cgrectmake(x,y,width,height))];
}
}
-(void)viewBecamePortrait:(id)sender{
}

How to build a bar on the keyboard

I want to dismiss keyboard by click dismiss button ,How to build a bar on the keyboard? like this:
(source: alexcurylo.com)
It was answered here. Basically, you send a resignFirstResponder message to the UITextView. Of course, you will put that code on your button's delegate.
Try to put following code. It might work for you.
Put following variables in your viewController.h file
UIToolbar *keyboardToolbar;
id notisender;
BOOL isAlreadyResigned;
float kx,ky,kh,kw;
Now put following code in you viewController.m file.
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
isAlreadyResigned=YES;
[self keyboardToolbarShouldShow];
[self keyboardToolbarShouldShow];
[super viewWillAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
[self keyboardToolbarShouldNotShow];
[super viewWillDisappear:animated];
}
#pragma mark keyboardWillShow methods
-(void)keyboardToolbarShouldShow {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardhide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)keyboardToolbarShouldNotShow {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
if(!isAlreadyResigned){
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillNotShow:)
name:UIKeyboardWillShowNotification
object:nil];
[GEEtxtMsg becomeFirstResponder];
}
}
-(void)keyboardWillShow : (NSNotification *)sender {
#try {
// NSLog(#"notification in first view");
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
for (UIView *keyboard in [keyboardWindow subviews]) {
NSLog(#"keyboard description - %#",[keyboard description]);
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
NSValue *v = [[sender userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
CGRect kbBounds = [v CGRectValue];
if(keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectZero];
keyboardToolbar.barStyle=UIBarStyleBlackOpaque;
keyboardToolbar.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(dismissKeyboard)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *items = [[NSArray alloc] initWithObjects:flex, barButtonItem, nil];
[keyboardToolbar setItems:items];
[items release];
}
[keyboardToolbar removeFromSuperview];
keyboardToolbar.frame = CGRectMake(0, 0, kbBounds.size.width, 45);
[keyboard addSubview:keyboardToolbar];
NSLog(#"x=%f y=%f width=%f height=%f",kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height);
kx=kbBounds.origin.x; ky=kbBounds.origin.y;
kh=kbBounds.size.height; kw=kbBounds.size.width;
keyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height + 87);
isAlreadyResigned=NO;
for(UIView* subKeyboard in [keyboard subviews]) {
if([[subKeyboard description] hasPrefix:#"<UIKeyboardImpl"] == YES) {
subKeyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y - 45, kbBounds.size.width, kbBounds.size.height);
}
}
}
}
}
} #catch (NSException * e) {
NSLog(#"Problem in keyboardWillShow:%#",e);
}
}
-(void)keyboardWillNotShow : (NSNotification *)sender {
#try {
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
for (UIView *keyboard in [keyboardWindow subviews]) {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
// NSValue *v = [[sender userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
// CGRect kbBounds = [v CGRectValue];
if([[keyboard subviews] containsObject:keyboardToolbar]){
[keyboardToolbar removeFromSuperview];
}
if(!isAlreadyResigned){
isAlreadyResigned=YES;
keyboard.bounds = CGRectMake(kx,ky,kw,kh);
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
for(UIView* subKeyboard in [keyboard subviews]) {
if([[subKeyboard description] hasPrefix:#"<UIKeyboardImpl"] == YES) {
subKeyboard.bounds = CGRectMake(0, 0,0, 0);
}
}
}
}
}
}
} #catch (NSException * e) {
NSLog(#"Problem in keyboardWillShow:%#",e);
}
}
-(void)dismissKeyboard {
[self resignTextFields];
}
-(void)keyboardhide:(NSNotification *)noti {
notisender = noti;
}
Here you can build a bar on a keyboard
Take static toolbar on xib and take one button and put it on toolbar, and make toolbar hidden ,now
write code in your method when you create a toolbar ,like if you want to create toolbar on textview begin editing method then,
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.26];
[tlBar setFrame:CGRectMake(0, 220, 320, 44)];
tlBar.hidden=NO;
[UIView commitAnimations];
}
And when you dismiss keyboard write this method and call this method when toolbar button pressed
-(IBAction)kbAway:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[tlBar setFrame:CGRectMake(0, 480, 320, 44)];
[UIView commitAnimations];
[txtviewmessage resignFirstResponder];
[txtsubject resignFirstResponder];
[txttemplatename resignFirstResponder];
}
Hope this will help you..