Ios UIScrollView自动重新加载子视图位置
我有一个带有Ios UIScrollView自动重新加载子视图位置,ios,iphone,objective-c,uiscrollview,Ios,Iphone,Objective C,Uiscrollview,我有一个带有子视图的滚动视图子视图是集合视图和其他视图。当用户单击集合视图中的添加按钮时,新项目被添加到集合视图,在我为另一个子视图更新新框架后,我更新了集合视图的新框架(1行3个项目)。一切正常。但是当我滚动滚动视图时,所有子视图都返回旧位置!我不知道发生了什么事 - (void)buttonAddPressed:(NSIndexPath *)indexPath { [_productImages addObject:[UIImage imageNamed:@"4.jpg"]];
子视图的滚动视图
<代码>子视图
是集合视图
和其他视图。当用户单击集合视图
中的添加按钮时,新项目被添加到集合视图
,在我为另一个子视图
更新新框架后,我更新了集合视图
的新框架(1行3个项目)。一切正常。但是当我滚动滚动视图
时,所有子视图
都返回旧位置!我不知道发生了什么事
- (void)buttonAddPressed:(NSIndexPath *)indexPath {
[_productImages addObject:[UIImage imageNamed:@"4.jpg"]];
[_collectionView insertItemsAtIndexPaths:@[indexPath]];
double delayInSeconds = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
float oldHeight = _collectionView.frame.size.height;
float newHeight = _collectionView.contentSize.height;
NSLog(@"new height %f",newHeight);
[UIView animateWithDuration:0.1 animations:^{
CGRect collectionViewFrame = _collectionView.frame;
collectionViewFrame.size.height = newHeight;
_collectionView.frame = collectionViewFrame;
}];
[self scrollViewChangeHeight:(float)(newHeight - oldHeight)];
});
}
- (void)scrollViewChangeHeight:(float)heightadd {
// thay doi chieu cao cua scrollview
CGSize scrollViewContentSize = _scrollView.contentSize;
scrollViewContentSize.height += heightadd;
[_scrollView setContentSize:scrollViewContentSize];
// di chuyen cac phan tu ben trong
[UIView animateWithDuration:0.1 animations:^{
for (UIView *view in _viewsUnderCollectionView) {
CGRect viewFrame = view.frame;
viewFrame.origin.y += heightadd;
view.frame = viewFrame;
}
}];
}
你能展示一下你是如何在UIScrollView上添加_collectionView和其他视图的吗?@nikhilgohil11:我在情节提要中添加了它们,然后在viewDidAppear方法中设置了CrollViewContentSize!您的scrollview大小或contentsize再次重置为旧帧。您可以使用NSLog in–scrollViewDidScroll:delegate方法并检查您的scrollview的内容大小是否与您在ViewDidDisplay中设置的内容大小相同:@nikhilgohil11:谢谢您的回答,但我的scroll view没有重置帧,而是它的子视图重置旧位置!