I have a scrollview
with subviews
. Subviews
are collectionview
and other views.when user click to add button in collectionview
, new item's added to collectionview
and i update new frame for collectionview
( 3 item for 1 row ) , after i update new frame for another subviews
.Everything is ok . But When i scroll the scrollview
, all subviews
return old position ! i don't know what is happening !
- (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;
}
}];
}