Working with keyboard in Objective C - Part 1
Main Methods
- Displaying Keyboard
- Dismiss Keyboard
--------------------------------------------------------------------------------------
01. Displaying keyboard when view open
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any
additional setup after loading the view from its nib.
[self.nameTextField becomeFirstResponder];
}
02. Keyboard to hide when the user taps outside of the text field
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.nameTextField resignFirstResponder];
[self.emailTextField resignFirstResponder];
[self.phoneNumberTextField resignFirstResponder];
}
03. Keyboard to hide when you press the return or done button of the keyboard
.h
--------------------------------------------------------------------------------------------------
#import <UIKit/UIKit.h>
@interface KeyboardViewController : UIViewController <UITextFieldDelegate>
@property(nonatomic,retain)IBOutlet UITextField *nameTextField;
@property(nonatomic,retain)IBOutlet UITextField *emailTextField;
@property(nonatomic,retain)IBOutlet UITextField *phoneNumberTextField;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any
additional setup after loading the view from its nib.
[self.nameTextField becomeFirstResponder];
self.nameTextField.delegate = self;
self.phoneNumberTextField.delegate = self;
self.emailTextField.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.nameTextField resignFirstResponder];
[self.emailTextField resignFirstResponder];
[self.phoneNumberTextField resignFirstResponder];
return NO;
}
--------------------------------------------------------------------------------------------------------------
04. UITextField move up when keyboard is present
.h
--------------------------------------------------------------------------------------------------
@interface KeyboardViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic,retain)IBOutlet UITextField *nameTextField;
@property(nonatomic,retain)IBOutlet UITextField *emailTextField;
@property(nonatomic,retain)IBOutlet UITextField *phoneNumberTextField;
@property(nonatomic,retain)IBOutlet UIScrollView *backGroundScroll;
@property(nonatomic,retain)IBOutlet UIButton *registerButton;
@end
#import "KeyboardViewController.h"
@interface KeyboardViewController ()
@end
@implementation KeyboardViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom
initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any
additional setup after loading the view from its nib.
self.nameTextField.delegate = self;
self.phoneNumberTextField.delegate = self;
self.emailTextField.delegate = self;
[self.nameTextField becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.nameTextField resignFirstResponder];
[self.emailTextField resignFirstResponder];
[self.phoneNumberTextField resignFirstResponder];
return NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose
of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self registerForKeyboardNotifications];
}
- (void)viewWillDisappear:(BOOL)animated {
[self deregisterFromKeyboardNotifications];
[super viewWillDisappear:animated];
}
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)deregisterFromKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification
*)notification {
NSDictionary* info =
[notification userInfo];
CGSize keyboardSize
= [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGPoint buttonOrigin
= self.registerButton.frame.origin;
CGFloat buttonHeight
= self.registerButton.frame.size.height;
CGRect visibleRect =
self.view.frame;
visibleRect.size.height -=
keyboardSize.height;
if (!CGRectContainsPoint(visibleRect,
buttonOrigin)){
CGPoint scrollPoint =
CGPointMake(0.0,
buttonOrigin.y - visibleRect.size.height + buttonHeight);
[self.backGroundScroll setContentOffset:scrollPoint animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification
*)notification {
[self.backGroundScroll setContentOffset:CGPointZero animated:YES];
}
@end
--------------------------------------------------------------------------------------------------------------------
Tutorial List
- SQLite Based iPhone Application
- json with IOS (Basic) - Tutorial
- Simple Table View in IOS
- Load Distinct Images from array into UITableView
- Simple UIPickerView Example
- Tab Bar Application Part 1
- Tab Bar Application Part 2
- Tab Bar Application Part 3 ( with Login Screen )
- Tab Bar Application Part 4 ( Navigation )
- Table View Design in IOS Part 1
- Table View Design in IOS Part 2 ( Time Table )
- How to Add search bar in Table View
- Local Notification in IOS
- Core Data (For Beginners )
- Core Data Tute 2 (Add/Delete/Search)
- Core Data Tute 3 (Two table)
- Custom Cell in UITableview
- Lazy Loading
- Pull to Refresh in TableView
- Working with keyboard in Objective C - Part 1
No comments:
Post a Comment