diff --git a/Classes/AQGridView.m b/Classes/AQGridView.m index 3f62735..996a2ff 100755 --- a/Classes/AQGridView.m +++ b/Classes/AQGridView.m @@ -459,9 +459,16 @@ - (void) updateContentRectWithOldMaxLocation: (CGPoint) oldMaxLocation gridSize: - (void) handleGridViewBoundsChanged: (CGRect) oldBounds toNewBounds: (CGRect) bounds { + CGPoint oldOffset = self.contentOffset; CGSize oldGridSize = [_gridData sizeForEntireGrid]; BOOL wasAtBottom = ((oldGridSize.height != 0.0) && (CGRectGetMaxY(oldBounds) == oldGridSize.height)); + // cell height may change on rotation + if ( _flags.dataSourceGridCellSize == 1 ) + { + [_gridData setDesiredCellSize: [_dataSource portraitGridCellSizeForGridView: self]]; + } + [_gridData gridViewDidChangeBoundsSize: bounds.size]; _flags.numColumns = [_gridData numberOfItemsPerRow]; CGSize newGridSize = [_gridData sizeForEntireGrid]; @@ -478,6 +485,19 @@ - (void) handleGridViewBoundsChanged: (CGRect) oldBounds toNewBounds: (CGRect) b self.contentOffset = contentRect.origin; } } + else if (!wasAtBottom && !CGPointEqualToPoint(oldBounds.origin, CGPointZero) && !CGSizeEqualToSize(oldGridSize, CGSizeZero)) + { + // If not at the bottom or top (they are handled above and in updateContentRectWithOldMaxLocation) + // and we have data to work with (sometimes we can start with the oldSize == 0), + // constentOffset.y needs to be reset to be proportional to the new height. Then snap it to the nearest cell boundry. + + CGFloat proportionalY = newGridSize.height * (oldOffset.y / oldGridSize.height); + CGRect cellRect = [_gridData cellRectForPoint:CGPointMake(self.contentOffset.x, proportionalY)]; + CGFloat newY = (proportionalY < (cellRect.origin.y + (cellRect.size.height / 2.0f))) ? + cellRect.origin.y : cellRect.origin.y + cellRect.size.height; + self.contentOffset = CGPointMake(self.contentOffset.x, newY); + } + [self updateVisibleGridCellsNow]; _flags.allCellsNeedLayout = 1; @@ -545,7 +565,7 @@ - (void) setFrame: (CGRect) newFrame [super setFrame: newFrame]; CGRect newBounds = self.bounds; - if ( newBounds.size.width != oldBounds.size.width ) + if ( !CGSizeEqualToSize(newBounds.size, oldBounds.size) ) [self handleGridViewBoundsChanged: oldBounds toNewBounds: newBounds]; }