Search code examples
iosios4

-80 being added to bounds.origin.x for UIView


I have a series of UIViews inside a UIScrollView which I am attempting to resize when the root view controller receives willAnimateRotationToInterfaceOrientation:duration:

Here is the code...

(Panel is just an extended UIView; panels is an NSMutableArray of Panels held by an extended UIScrollView)

for(Panel * panel in panels)
{
  panel.bounds = CGRectMake(panel.bounds.origin.x,
                            panel.bounds.origin.y,
                            self.bounds.size.width,
                            panel.bounds.size.height);
}

NSLog(@"%@", [panels description]);

The self.bounds.size.width is the UIScrollView (I've extended the class).

Here is the result of NSLog when the device is oriented to landscape...

"<Panel: 0x74324d0; frame = (-80 0; 480 50); animations = { bounds=<CABasicAnimation: 0xe1339d0>; }; layer = <CALayer: 0x7432530>>",
"<Panel: 0x7435ce0; frame = (-80 53; 480 50); animations = { bounds=<CABasicAnimation: 0xe133a90>; }; layer = <CALayer: 0x7435d40>>",
"<Panel: 0x7438e20; frame = (-80 106; 480 50); animations = { bounds=<CABasicAnimation: 0xe133ab0>; }; layer = <CALayer: 0x7438e80>>",
"<Panel: 0x7438eb0; frame = (-80 159; 480 50); animations = { bounds=<CABasicAnimation: 0x714f890>; }; layer = <CALayer: 0x7438f10>>",
"<Panel: 0x7438f40; frame = (-80 212; 480 50); animations = { bounds=<CABasicAnimation: 0x742ffd0>; }; layer = <CALayer: 0x7438fa0>>"

And when it's changed back to portrait...

"<Panel: 0x7480350; frame = (0 0; 320 50); animations = { bounds=<CABasicAnimation: 0x74a3b40>; }; layer = <CALayer: 0x74803b0>>",
"<Panel: 0x7483c90; frame = (0 53; 320 50); animations = { bounds=<CABasicAnimation: 0x74a3bd0>; }; layer = <CALayer: 0x7483cf0>>",
"<Panel: 0x7486dd0; frame = (0 106; 320 50); animations = { bounds=<CABasicAnimation: 0x74a3c60>; }; layer = <CALayer: 0x7486e30>>",
"<Panel: 0x7486e60; frame = (0 159; 320 50); animations = { bounds=<CABasicAnimation: 0x74a3cf0>; }; layer = <CALayer: 0x7486ec0>>",
"<Panel: 0x7486ef0; frame = (0 212; 320 50); animations = { bounds=<CABasicAnimation: 0x74a3d80>; }; layer = <CALayer: 0x7486f50>>"

I don't see any reason why the bounds.origin.x should ever be anything other than what it starts at (0). It must be something obvious, but I just can't see it - maybe some fresh eyes will see what I'm doing wrong.


Solution

  • I think you should have a look at Difference between frame and bounds

    You have to set frame not bounds, as setting the bounds changes frame of view in its superView.

    Hope this will help.