s1m0n
s1m0n

Reputation: 7685

UIView has portrait dimensions while in landscape?

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.

Upvotes: 5

Views: 6201

Answers (4)

George
George

Reputation: 4029

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

[myView setAutoresizingMask:UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleRightMargin];

Upvotes: 2

chandu
chandu

Reputation: 476

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.

Upvotes: 0

A-Live
A-Live

Reputation: 8944

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.

Upvotes: 11

Sumanth
Sumanth

Reputation: 4921

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);    
}

Upvotes: 0

Related Questions