开发者

Problem with scrollview, it scrolls more

I have 开发者_开发技巧a scrollView and i want to scroll it automatically when i select an textField

(i am filling a form here)

i am using following method to scroll it

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{   
[scrollView setContentOffset:CGPointMake(0,50*(textField.tag-1))];
}

Now the problem is that i have more than 10 text fields and when i reached to the seventh textField the scrollView scrolls more . I also tried to print the CGPointMake()'s values...and it is showing correct values..but the scroller goes beyond the range what is expected.. Look at following images

The following 2 images showing control on textFields tag <7

Problem with scrollview, it scrolls more

Problem with scrollview, it scrolls more

But when control reaches to 7th textField it scrolls more

Problem with scrollview, it scrolls more

and after this it goes beyond bounds.

This problem occurs only when i move from one textFields to another without pressing that return button(i mean with resignFirstResponder).But when i press that return button and then go to the next field then all works fine.

Can anyone suggest where should be the problem..?


It won't scroll because it doesn't contain enough content. It will stop when the bottom edge of the content reaches the bottom end of the frame. Use something like

CGSize size = scrollView.contentSize;
scrollView.contentSize = CGSizeMake (size.width, size.height + ADDITIONAL_HEIGHT);

when setting up the scroll view, or in your -viewDidLoad: method, if it was loaded from a XIB.


It should keep the text field being edited on the screen - i.e. scroll automatically - so you shouldn't need to do anything?

However, it might be trying to edit a text field that is behind the keyboard - so the better solution would be:

//Get notifications of the keyboard opening and closing
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

- (void)keyboardWillShow:(NSNotification *)notification {
    //Get the keyboard height
    int h = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size.height;
    //Change the inset of the scroll view and scroll bars
    scrollView.contentInset = scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, h, 0);
}
- (void)keyboardWillHide:(NSNotification *)notification {
    scrollView.contentInset = scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}


try this.declare a variable

     CGPoint svos;

in .h file and do this.

       - (void)textFieldDidBeginEditing:(UITextField *)textField {
svos = scr.contentOffset;//scr is my scroll view
CGPoint pt;
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:scr];
pt = rc.origin;
pt.x = 0;
pt.y -= 60;
[scr setContentOffset:pt animated:YES];   
//NSLog(@"%f",pt.y);
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜