Problem with setContentOffset: animated: YES

I have a problem with what almost looks like a bug in iOS. I am trying to do a simple scroll in a UIScrollView. If I go to the point with the animation, it scrolls fine there, but it does not set the point in scrollView. I.E. when I move to another place later, it jumps to 0.0 and starts the animation from there.

I am using the following code

[scrollView setContentOffset:CGPointMake(0, 95) animated:YES];
NSLog(@"offset x %@", [[NSNumber numberWithFloat:scrollView.contentOffset.x] stringValue]);
NSLog(@"offset y %@", [[NSNumber numberWithFloat:scrollView.contentOffset.y] stringValue]);

which produces the output

offset x 0
offset y 0

while the same code with animation disabled:

[scrollView setContentOffset:CGPointMake(0, 95) animated:NO];
NSLog(@"offset x %@", [[NSNumber numberWithFloat:scrollView.contentOffset.x] stringValue]);
NSLog(@"offset y %@", [[NSNumber numberWithFloat:scrollView.contentOffset.y] stringValue]);

outputs a conclusion

offset x 0
offset y 95

I'm trying to automatically scroll to a UITextView, so I listen to some keyboard notifications, where I usually scroll. But I did this test in viewDidLoad, and it produces these results.

scrollView.contentOffset = CGPointMake (0,95); . , .

: , , :

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    //60 is half the available space in portrait mode so it puts the textfield in the centre.
    [self.scrollView setContentOffset:CGPointMake(0, textField.frame.origin.y-60) animated:YES];
}

. , , contentOffset, 0,0 . , .

+3
3

, . . ,

[scrollView setContentOffset:CGPointMake(0, 95) animated:YES];

, scrollView, . , , . scrollViewDidScroll:, scrollViewDidEndScrollingAnimation: ..

, , , , , . , , .

--- , ,

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

tableViewController. , setOffset viewDidLoad.

--- textView - , UIAlertView, , setTransform: UIAlertView. , ​​ 4.2.

+1

, . , , 0, , .

0

Customizing the animated: YES you are using CoreAnimation, and not immediately installing your content.

try the following:

[scrollView setDelegate:self];

and then

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // Do something
}
0
source

All Articles