I have a view controller that starts in landscape mode with a UIScrollView in it. I try to create subviews and add them to the UIScrollView but the frame size of the views were all in portrait size.
Here's my code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.scrollView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++)
{
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * i;
frame.origin.y = 0;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count,
self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;
}
The output:
You can try this code. The values used are tweakable based on your requirement. If you try to set the frame on your own with rotations make sure that you dont use the autoresizing mask or else everything can go wrong. Hope that answers to your question.