5

I have a storyboard with 1 UIViewController, holding 1 UIView that contains a number of nested UIViews. I subclassed the View Controller to implement this method:

- (BOOL)shouldAutorotateToInterfaceOrientation: UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

I also added

<key>UISupportedInterfaceOrientations</key>
    <array>
     <string>UIInterfaceOrientationLandscapeLeft</string>
     <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
<key>UIInterfaceOrientation</key>
<string>UIInterfaceOrientationLandscapeLeft</string>

to the Info.plist.

In the viewDidLoad of the main UIView I'm doing this:

PASectionView* sectionView = [[PASectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 180)];
[self addSubview:sectionView];

The problem is the control is only 756px wide instead of the expected 1024. See the screenshot below for details. enter image description hereI've been searching all over the web but I can't find a solution to this frustrating problem anywhere. I'm using Xcode 4.5 with iOS5.1 set as base SDK.

EDIT It's working by replacing frame with bounds. However I don't understand what's happening so it isn't working with the frame size.

s1m0n
  • 7,825
  • 1
  • 32
  • 45

4 Answers4

11

The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.

@property(nonatomic) CGRect frame

and

The bounds rectangle, which describes the view’s location and size in its own coordinate system.

@property(nonatomic) CGRect bounds

Use bounds, not frame.

A-Live
  • 8,904
  • 2
  • 39
  • 74
  • Thanks! Replacing 'frame' with bounds works! However, I don't really understand why? Any chance to make it work with frame? Don't understand why the size of the UIView is different in the superview's and in its own coordinate system – s1m0n Jul 09 '12 at 16:14
  • From my understanding that happens because the view lies on the window and that window must have an internal landscape mode, if that's somehow close to be true, the view coordinates at the window coordinate system doesn't reflect the rotation as the window is rotated together with it's view and it's sub view's. You might want to refresh the coordinate system knowledge for the better understanding (rotation and maybe transition must be enough for this case): http://en.wikipedia.org/wiki/Cartesian_coordinate_system That's a school math as i remember. – A-Live Jul 09 '12 at 22:53
  • 1
    It would probably be worth reading through http://stackoverflow.com/questions/1210047/iphone-development-whats-the-difference-between-the-frame-and-the-bounds, and some of the linked questions. – Martin Kenny Jul 10 '12 at 15:01
2

Set the autoresizingMask to whatever views you want to autoresize on rotate. Like this:

[myView setAutoresizingMask:UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleRightMargin];
George
  • 4,029
  • 1
  • 23
  • 31
  • 1
    Thanks for the answer. But I'm not trying to let my app autorotate or something. Landscape mode is enough for me. So it shouldn't autorotate or whatever, I just want it to return the correct landscape dimensions. – s1m0n Jul 09 '12 at 12:45
0

The problem is because before loading into Landscape mode your lines of code is calling the method loading in which method are you calling this [PASectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 180)]; [self addSubview:sectionView]; if it is view controller class then it should be [PASectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)]; [self addSubview:sectionView]; if it is subclass of UIView then where did you get the ViewDidLoad method. Now to solve your problem in .h class: write this PASectionView* sectionView; in .m class implement this method

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{

}
else  // OrientationLandscape
{
sectionView.frame = CGRect(sectionView.frame.origin.x,sectionView.frame.origin.y,self.view.frame.size.width,180);    
}
Sumanth
  • 4,913
  • 1
  • 24
  • 39
0

You have to set the Auto resizing mask for your subview,

Add this line of code before adding the subView to the superview, if your subView is added by code. yourSubView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

else if your subView added in nib file, select the border magnets and resizing masks in the properties of your subView in the nib file.

chandu
  • 476
  • 5
  • 12