0
我有一個scrollview
與subviews
。 Subviews
是collectionview
和其他views.when用戶點擊添加按鈕在collectionview
,新項目的添加到collectionview
和我更新新幀爲collectionview
(3項目爲1行),後我更新新幀爲另一個subviews
。一切都很好。但是,當我滾動scrollview
,所有subviews
返回舊的位置!我不知道發生了什麼!UIScrollView自動重新加載子視圖位置
- (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
@ nikhilgohil11:我將它們添加到故事板中,然後我在viewDidAppear方法中設置了ScrollViewContentSize! –
您的滾動視圖的大小或內容大小再次重置爲舊框架。你可以使用NSLog - scrollViewDidScroll:delegate方法並檢查你的scrollview的內容大小是否與你在viewDidAppear中設置的相同: – nikhilgohil11