0

In my app i have 8 Text-fields.I have implemented scrollview when keyboard appears,but when i start entering text in the 1st textfield the view scrolls and it hides my current focused Text-field.I want that the scroll should happen when i am entering text in some particular text-field suppose 5th onwards. How can i do so.

1
  • There are a number of quality responses to your question. Might make sense to edit your title given that "particular case" is very generic. Commented Apr 13, 2015 at 13:14

4 Answers 4

1

Use NSNotifications to know the height of the keyboard:

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

and then implement this function to get the height of the keyboard:

-(void)showKeyBoard:(NSNotification *)notification
{

NSDictionary *info=[notification userInfo];
 keyboardSize=[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

}

This will give you the size of the keyboard.

Now use set the tags of the textfields in increasing order of their appearance, and set your view controller as their delegate.

Then use this delegate method to set the contentOffset of the scrollView:

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
}
Sign up to request clarification or add additional context in comments.

Comments

1

hi i have used this code from long time and it is very useful for scrollview and tableview

#pragma mark
#pragma mark - text field delegate method
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self showKeyBoard:YES];
    [self setTableOffsetForTextField:textField];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self.view endEditing:YES];
    [self showKeyBoard:NO];
    return YES;
}
#pragma mark - Show Hide Key Board
- (void)showKeyBoard:(BOOL)boolValue
{
    scrollView.contentInset = UIEdgeInsetsMake(0, 0, boolValue ? ([self kbHeight1]) : 0.0, 0.0);
}


- (CGFloat)kbHeight1
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

        if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
            return 264.0;
        }
        else
        {
            return 352.0;
        }
    }
    else
    {
        return 216.0;
    }

}
- (void)setTableOffsetForTextField:(UIView *)textField
{

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)){

        return;
    }


    CGPoint point = [textField convertPoint:textField.frame.origin toView:scrollView];
    CGFloat diff = [self difference];
    CGFloat pos = (point.y - diff);
    if (pos < 0 || pos > scrollView.contentSize.height - diff) {
        pos = 0;
    }
    [scrollView setContentOffset:CGPointMake(0, pos) animated:YES];
}


-(CGFloat)difference{
    CGSize screenSize = self.view.frame.size;

    CGFloat diff;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        diff = (screenSize.height > 768) ? 264.0 : 250;

    }
    else
    {
        diff = (screenSize.height == 480) ? 120.0 : 70.0;        
    }
    return diff;
}

may this help you.

Comments

0

implement method of UITextFieldDelegate:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [self.scroolView scrollRectToVisible:textfield.frame animated:YES]; 
}

You can implement more custom logic to calculate visibleFrame in this method.

Comments

0

You can do it using tags.

Put tags to each text field as 1-8 for 8 textfields. Check the tag value for text field if it is equal or greater than 5 then only apply your scrollview else return;

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if(textField.tag==5 || textField.tag>=5)
       {
       //Implement scrollview here
       }
}

Hope it helps you...!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.