Reputation: 7685
I have a storyboard with 1 UIViewController
, holding 1 UIView
that contains a number of nested UIView
s. 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. I'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
Reputation: 4029
Set the autoresizingMask
to whatever views you want to autoresize on rotate. Like this:
[myView setAutoresizingMask:UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleRightMargin];
Upvotes: 2
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
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
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