0

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;
        }
    }];
}
Sport
  • 8,570
  • 6
  • 46
  • 65
  • can you please show how you have added _collectionView and other view on UIScrollView? – nikhilgohil11 Dec 15 '13 at 11:05
  • @nikhilgohil11 : i added them in storyboard , then i setScrollViewContentSize in viewDidAppear method ! – Chau Duong Khong Dec 15 '13 at 11:35
  • Either your scrollview' size or contentsize is again resetting to old frame. you can use NSLog in – scrollViewDidScroll: delegate method and check your scrollview's content size is same as what you have set in viewDidAppear: – nikhilgohil11 Dec 15 '13 at 12:13
  • @nikhilgohil11 : thanks for your answer , but my scroll view not resetting frame , but subviews of it resetting old position ! – Chau Duong Khong Dec 15 '13 at 12:28

0 Answers0