Hi all, Today i want to share with all Iphone developers.
In this post My topic is ,
How to handle view positions when keyboard hide and show time in Iphone:
1)KeyBoardViewController.xib you need to set delegate to all UItextFields
2)In the KeyBoardViewController.h file, add the following code in bold:
3)In the KeyBoardViewController.m file, add the following code in bold:
Thank you
In this post My topic is ,
How to handle view positions when keyboard hide and show time in Iphone:
1)KeyBoardViewController.xib you need to set delegate to all UItextFields
2)In the KeyBoardViewController.h file, add the following code in bold:
#import <UIKit/UIKit.h>
@interface KeyBoardViewController : UIViewController<UIScrollViewDelegate,UITextFieldDelegate>
{
IBOutlet UIScrollView *scrollView;
UITextField *currentTextField;
BOOL keyboardIsShown;
}
@property (nonatomic, retain) UIScrollView *scrollView;
@end
3)In the KeyBoardViewController.m file, add the following code in bold:
#import "KeyBoardViewController.h"
@interface KeyBoardViewController ()
@end
@implementation KeyBoardViewController
@synthesize scrollView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
scrollView.frame = CGRectMake(0, 0, 320, 460);
[scrollView setContentSize:CGSizeMake(320, 480)];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//—-before the View window appears—-
-(void) viewWillAppear:(BOOL)animated
{
//—-registers the notifications for keyboard—-
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification object:self.view.window];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification object:self.view.window];
[super viewWillAppear:animated];
}
//—-when a
-(void) textFieldDidBeginEditing:(UITextField *)textFieldView
{
currentTextField = textFieldView;
}
//—-when the user taps on the return key on the keyboard—-
-(BOOL) textFieldShouldReturn:(UITextField *) textFieldView
{
[textFieldView resignFirstResponder];
return NO;
}
//—-when a TextField view is done editing—-
-(void) textFieldDidEndEditing:(UITextField *) textFieldView
{
currentTextField = nil;
}
-(void) keyboardDidShow:(NSNotification *) notification
{
if (keyboardIsShown) return;
NSDictionary* info = [notification userInfo];
//—-obtain the size of the keyboard—-
NSValue *aValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect =[self.view convertRect:[aValue CGRectValue] fromView:nil];
NSLog(@"%f", keyboardRect.size.height);
//—-resize the scroll view (with keyboard)—-
CGRect viewFrame = [scrollView frame];
viewFrame.size.height -= keyboardRect.size.height;
scrollView.frame = viewFrame;
//—-scroll to the current text field—-
CGRect textFieldRect = [currentTextField frame];
[scrollView scrollRectToVisible:textFieldRect animated:YES];
keyboardIsShown = YES;
}
//—-when the keyboard disappears—-
-(void) keyboardDidHide:(NSNotification *) notification {
NSDictionary* info = [notification userInfo];
//—-obtain the size of the keyboard—-
NSValue* aValue =[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect =[self.view convertRect:[aValue CGRectValue] fromView:nil];
//—-resize the scroll view back to the original size // (without keyboard)—-
CGRect viewFrame = [scrollView frame]; viewFrame.size.height += keyboardRect.size.height; scrollView.frame = viewFrame;
keyboardIsShown = NO;
}
//—-before the View window disappear—-
-(void) viewWillDisappear:(BOOL)animated { //—-removes the notifications for keyboard—-
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidHideNotification object:nil];
[super viewWillDisappear:animated];
}
@end
Thank you
No comments:
Post a Comment