layoutSubviews doesn't always work (iOS 4.3 on iPad Simulator)
layoutSubviews doesn't always work (iOS 4.3 on iPad Simulator)
- Subject: layoutSubviews doesn't always work (iOS 4.3 on iPad Simulator)
- From: Brian Bruinewoud <email@hidden>
- Date: Sun, 12 Jun 2011 14:30:42 +1000
Hi All,
I have an app that consists of a scroll view subclass which contains a single subview. In the scroll view subclass I override layoutSubviews based on Apple sample code (see below). The intention of layoutSubviews is to centre the subview in the scrollview when the subview is smaller than the scrollview's display area.
There are three circumstances where the layoutSubviews is called but in one of them the visual results are incorrect.
If I pinch to zoom, the layout looks correct.
scrollViewDidEndZooming:withView:forScaleL contains a single line of code that calls rescaleTo:animated:
[ rescaleTo: myScale * scale animated: NO ];
If I change the frame of the subview, the layout looks correct.
This is called automatically by the framework because I have called setNeedsLayout when I resized the subview.
If I attempt to reset the zoom to scale = 1, the layout is not centred if the view was bigger than the display area prior to attempting the rescale.
This is done by calling rescaleTo:animated: directly.
[ rescaleTo: 1.0 animated: YES ];
The animated: parameter only affects the contents of the subview and setting it to NO for all calls to this method doesn't change the results.
When I trace through layoutSubviews, the input (self.bounds.size and myCurrentView.frame) and output (frameToCentre) values are the same in all three cases, but only in the third does it not centre properly. Note that the subview does change position, though.
Here is the code for layoutSubviews:
- (void)layoutSubviews
{
[super layoutSubviews];
// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size; // 1024 x 704
CGRect frameToCenter = myCurrentView.frame; // 240 x 100 @ ( 0, 0 )
// center horizontally
if( frameToCenter.size.width < boundsSize.width )
frameToCenter.origin.x = ( boundsSize.width - frameToCenter.size.width ) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if( frameToCenter.size.height < boundsSize.height )
frameToCenter.origin.y = ( boundsSize.height - frameToCenter.size.height ) / 2;
else
frameToCenter.origin.y = 0;
myCurrentView.frame = frameToCenter; // 240 x 100 @ ( 392, 302 )
}
Any ideas where I should look or what might be causing the behaviour?
Thanks,
Brian._______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden