简体   繁体   中英

Change UICollectionViewCell subview frame

I have a UICollectionView subclass with several subviews. The cell class includes a method which should cause animation of one of the subviews within the cell (timerView). The problem is that although the NSLog() statements suggest that the frame is being changed, nothing happens. There is no change to the frame of the subview. How can I cause the frame of the subView to change?

UIView *superView = self.timerView.superview;

//Force the border view to layout so we can get the size
[superView layoutIfNeeded];

NSLog(@"%@", NSStringFromCGRect(superView.frame));


self.timerView.frame = CGRectMake(0, 0, 0, superView.frame.size.height);

NSLog(@"%@", NSStringFromCGRect(self.timerView.frame));

[UIView animateWithDuration:duration animations:^{
    self.timerView.frame = self.timerView.superview.frame;
} completion:^(BOOL finished) {
    if (completion) {
        completion();
    }
}];

Urgh, the answer was so simple but it took me hours to work it out. I was retrieving the reference to the target UICollectionViewCell in collectionView: didSelectItemAtIndexPath: , using collectionView: cellForItemAtIndexPath: . This meant that the cell which was being referenced was not always the cell I thought it was, because of dequeueing. In the end I created a custom delegate callback from the cell so I could have my own didSelectItem... method, which also passed the cell itself. That way I knew that the cell I was addressing would always be the one I thought it was, and the animation then started working.

I only realised this when I started having problems with properties I knew I had set becoming nil when accessed from other methods. So I realised a new cell was being dequeued.

Thanks for all the help, and sorry it was a bit of a wild goose chase.

Try to call [superView layoutIfNeeded]; right after self.timerView.frame = self.timerView.superview.frame; . You should always notify iOS when you need to redraw the views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM