Friday, July 4, 2014

Working with keyboard in Objective C - Part 1

                                            Working with keyboard in Objective C  -  Part 1 





    Main Methods        
  •     Displaying Keyboard 

                   becomeFirstResponder method
  •      Dismiss Keyboard 

                  resignFirstResponder method

--------------------------------------------------------------------------------------

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

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

 #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;

@property(nonatomic,retain)IBOutlet UIScrollView *backGroundScroll;
@property(nonatomic,retain)IBOutlet UIButton *registerButton;

@end



 ----------------------------------------------------------------------------------------------------
.m
------------------------------------------------------------------------------------------------------

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

No comments:

Post a Comment