uiscrollview的scrollrecttovisible方法无效

因为之前做UIScrollView翻页是喜欢讲contentSize的height设为0,所以今天碰到了使用scrollRectToVisible无效的问题。所以以后在使用scrollRectToVisible函数前需要注意这一情况。

并且在设置好contentSize之后, 初次进入界面是x axis会有迷之39px的左偏移;

解决办法:

self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.dataSource.count, self.scrollView.bounds.size.height);

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

// or
// [self.scrollView scrollRectToVisible:CGRectMake(0, 0, SCREEN_WIDTH, self.scrollView.bounds.size.height) animated:YES];