1

I have a UIScrollView with height - 500, have UITextFileds in position 0, 100, 200, 300, 400, 500 (1 UITextFileds, UITextFileds 2, 3 UITextFileds, UITextFileds aUITextFileds 4 and 5).

ContentOffset is in the position (0, 70). I click on the UITextField 4, how do I know the position of the TextField 4 contentOffset??

enter image description here

Update:

I clicked on UITextField 4 and how I know I have to go up Y pixels so that to UITextField not be hidden by the Keyboard?

enter image description here

PedroMeira
  • 99
  • 1
  • 9
  • It's the frame origin of the text field. You're working in terms of the content size (not the visible bounds). You can subtract the frame origin and content offset to find the difference if you want to... – Wain Oct 21 '13 at 17:19
  • ok! My problem is, I do not see how I will up the UIScrollView Y pixels when she is in the middle of UIScrollView not to stay hidden. . If the UITextField have flush against the bottom and I raise and down to UIScrollView @Wain – PedroMeira Oct 22 '13 at 10:05
  • Have you tried always scrolling the selected text view to the top? – Wain Oct 22 '13 at 11:19

3 Answers3

1

My code:

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}

-(void) viewWillAppear: (BOOL) animated {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)  name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{

}


- (void)setScrollView:(UIScrollView *)scrollView offsetToView:(UIView *)view
{

    CGRect contentFrame = CGRectMake(view.frame.origin.x, view.frame.origin.y,view.frame.size.width, view.frame.size.height);
    CGPoint contentOffset = CGPointMake(0, contentFrame.origin.y);

    scrollView.contentOffset = contentOffset;

}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self setScrollView:_scrolviewKeys offsetToView:textField];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    _scrolviewKeys.frame = (CGRect){
        _scrolviewKeys.frame.origin,(CGSize){
                _scrolviewKeys.frame.size.width,_scrolviewKeys.frame.size.height-kbSize.height}


    };
}

click in UITextView yellow

result:

enter image description here

enter image description here

PedroMeira
  • 99
  • 1
  • 9
  • Ok I updated my answer but this site is not about asking the other coding for you, you should try to understand and improve you coding skill ;) – Francescu Oct 22 '13 at 12:44
  • the contentOffset.height = textField.origin.y + scrollView.size.height - textField.size.height; – Francescu Oct 22 '13 at 12:45
  • scrollView.contentOffset.y or scrollView.frame.size.height, not contentOffset.height @Francescu – PedroMeira Oct 22 '13 at 13:01
  • I started to work with iOS a month ago, is already difficult to understand the things @Francescu – PedroMeira Oct 22 '13 at 13:05
0

It should be as simple as :

- (void)setScrollView:(UIScrollView *)scrollView bottomOffsetToView:(UIView *)view
{
   scrollView.contentOffset = CGPointMake(0, view.frame.origin.y + view.frame.size.height - 216);
}

then in your code set yourself as UITextFieldDelegate and implement :

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
     self.currentTextField = textField;
}

Update :

You ask two different things here.. For the keyboard register for keyboard notifications (in viewWillAppear for instance) :

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

and then change the scrollview frame according to the keyboard size :

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    self.scrollView.frame = (CGRect){self.scrollView.frame.origin,(CGSize){self.scrollView.frame.size.width,self.scrollView.frame.size.height-kbSize.height);
    if (self.currentTextField != nil)
    {
        [self setScrollView:self.scrollView bottomOffsetToView:self.currentTextField];
    }
}

Same behavior for keyboardWasHidden (resetting initial frame) and don't forget to remove Observer in viewDidDisappear.

PedroMeira
  • 99
  • 1
  • 9
Francescu
  • 16,974
  • 6
  • 49
  • 60
  • ok! My problem is, I do not see how I will up the UIScrollView Y pixels when she is in the middle of UIScrollView not to stay hidden. . If the UITextField have flush against the bottom and I raise and down to UIScrollView. @Francescu – PedroMeira Oct 22 '13 at 10:14
  • The keyboard question is totally different.. Just register to notifications and change your scroll view content offset or inset.. – Francescu Oct 22 '13 at 11:32
  • Error in line @Francescu : `scrollView.contentOffset = CGSizeMake(0,view.frame.origin.y+scrollView.frame.size.height-view.frame.size.height);` Erro: **Assingin to 'CGPoint' (AKA 'Struct CGPoint') from incompatible type 'CGSize'(aka 'struct CGSize')** – PedroMeira Oct 22 '13 at 12:48
  • replace CGSizeMake by CGPointMake in the first function. If it doesn't work I'll delete my answer. – Francescu Oct 22 '13 at 13:23
  • replaces scrollView.frame.size.height by KeyboardHeight and it works. @Francescu – PedroMeira Oct 22 '13 at 14:54
0

In iOS 7, the scrollView will automatically scroll the minimum amount to keep the textField visible. If you're on iOS 6, here's what you can do the mimic this same behavior.

Assuming you had adjusted the scrollView.contentInset after keyboard is shown like below, here's what I did in - (void)textFieldDidBeginEditing:(UITextField *)textField:

self.scrollView.contentInset = (UIEdgeInsets){.bottom = 216.0f};
self.scrollView.scrollIndicatorInsets = self.scrollView.contentInset;

CGFloat minY = CGRectGetMinY([self.view convertRect:textField.frame fromView:textField]);
if (minY > self.scrollView.contentInset.bottom) {
    CGFloat visibleHeight = CGRectGetHeight(self.scrollView.bounds) - self.scrollView.contentInset.bottom;
    [self.scrollView setContentOffset:CGPointMake(0.0f, CGRectGetMaxY([self.view convertRect:textField.frame fromView:textField]) - visibleHeight) animated:YES];
}

We first calculate the textField's frame's minY in reference to the scrollView's frame, then see if it's greater than the amount we insetted the scrollView. If it is, that mean's this part of the scrollView is now under the keyboard. Then we calculate the offset by taking into account the visibleHeight, which is what's visible of the scrollView after the keyboard is shown. Take the textField's frame's maxY and subtract visibleHeight to keep it align to the top of the keyboard, thus moving the scrollView just the minimum amount to keep it visible.

James Kuang
  • 10,710
  • 4
  • 28
  • 38