Sign up Screen, when keyboard show textfield go to up and when hide textfield go to down

UITextField, UIScrollView, UIKeyboard

– View controller in storyboard go to Show the atribute inspector set Simulated matrics Size iPhone 4-inch

– Add UISCrollView x = 0, y = 0, Width = 320, Height = 568

ScrollView_Autolayout1

– Add pin add new Constraints Top = 0,Down = 0,Right = -20,Left = -20

add constraints

ScrollView_Autolayout2

– Go to Autolayout seggestion – Update frame – Fix misplacement

ScrollView_Autolayout3

– Add UIView(View size will be What ever you want) x = 0, y = 0, Width = as per ScrollView (Or whatever you want), Height = 768(whatever you want)

– Add pin add new constraints Top = 0,Down = -200,Right = 0,Left = 0 and Tick Height then add constraints

ScrollView_Autolayout4

– Hold Command and cluck and drag mouse aero View to ScrollView and set Equal Width

ScrollView_Autolayout5

– In Document out line open View constraints Select bottom = View.bottom and go to Right Side show the size inspector set constant 0(Zero)

ScrollView_Autolayout6

– Drag and drop two label

ScrollView_Autolayout7

Select Sign UP label

Set Constraints Centre horizontaly in container

Sign up Screen1

Add pin top, Width, Height

Sign up Screen2

Click Drag and Drop from First name Textfield to abow label

Select Center Horizontally

Sign up Screen3

Add pin top, Width

Sign up Screen4

Click drag and drop from last name textfield to first name textfield

select Centre Horizontally

Add pin top, Width

Do same thing to Set constraints for all textfield

Click drag and drop from last name Submit button to country textfield

select Centre Horizontally

Add pin top, Width, Height

Set IBOutlet for ScrollView named

Set IBOutlet for all Textfield

Set tag for all Textfield

Set delegate for all Textfield

ViewController.h

<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *currentTextField;

ViewController.m

– (void)viewDidLoad {

    [super viewDidLoad];

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    

    UITapGestureRecognizer *aGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];

    aGest.numberOfTapsRequired = 1;

    [self.ScrlViewSignUpVC addGestureRecognizer:aGest];

    

    // Do any additional setup after loading the view, typically from a nib.

}

– (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self

                                                    name:UIKeyboardWillShowNotification

                                                  object:nil];

    

    [[NSNotificationCenter defaultCenter] removeObserver:self

                                                    name:UIKeyboardWillHideNotification

                                                  object:nil];

}

#pragma mark – Gester method

– (void)tapDetected {

    [self.currentTextField resignFirstResponder];

}

#pragma mark – UIKeyboard show/hide notification

– (void)keyboardWillShow:(NSNotification *)iNotification {

    

    NSLog(@”%f”, [iNotification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height);

    NSDictionary *aKeyInfo = iNotification.userInfo;

    NSValue *aRectValue = [aKeyInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];

    CGRect aRect = [aRectValue CGRectValue];

    if (self.currentTextField.tag == 5 || self.currentTextField.tag == 6 || self.currentTextField.tag == 7) {

        self.ScrlViewSignUpVC.contentOffset = CGPointMake(0, (aRect.size.height) – 50);

    }

}

– (void)keyboardWillHide:(NSNotification *)iNotification  {

    self.ScrlViewSignUpVC.contentOffset = CGPointMake(0, 0);

}

#pragma mark – UITextField delegate

– (BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];

    return YES;

}

– (void)textFieldDidBeginEditing:(UITextField *)textField {

    self.currentTextField = textField;

}

Click here to Download Sign up Screen or when keyboard show textfield go to up and when hide textfield go to down Sample Project

Leave a comment