1
votes

I am using a UIScrollView with Paging enabled and the following code to add subviews (core plot charts) to it.

The horizontal scrolling between the views works properly. However, when showing the second view and then rotating from landscape to portrait mode, the second view is shifted partly to the right and a portion of the first view's right hand side is shown on the left side, hence "destroying" the paging mode.

Could you help me with these issue please? I tried many alternatives, but can't find my bug. Thank you so much!

This is how my iPad screen looks after rotating to portrait mode with the second view:

Parts of first screen (large bars) shown on the left side of the second screen

:

This is my viewDidLoad method:

- (void)viewDidLoad {

self.scrollView.contentSize = CGSizeMake(768 * 2, 400);
chart1 = [[CPTGraphHostingView alloc]initWithFrame:CGRectMake(0, 0, 768, 400)];
chart2 = [[CPTGraphHostingView alloc]initWithFrame:CGRectMake(768, 0, 768, 400)];

self.scrollView.autoresizesSubviews = NO;
chart1.autoresizesSubviews = YES;
chart2.autoresizesSubviews = YES;
self.scrollView.contentOffset = CGPointZero;
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth ;

chart1.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth ;
chart2.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth ;
[self.scrollView addSubview:chart1];
[self.scrollView addSubview:chart2];

}

This is how I have implemented rotation:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

 {
    if  (UIDeviceOrientationIsPortrait(fromInterfaceOrientation)) {
        self.scrollView.contentSize = CGSizeMake(704 * 2, 400);
        chart1.frame = CGRectMake(0, 0, 704, 400);
        chart2.frame = CGRectMake(704, 0, 704, 400);
        }
    else {
        self.scrollView.contentSize = CGSizeMake(768 * 2, 400);
        chart1.frame = CGRectMake(0, 0, 768, 400);
        chart2.frame = CGRectMake(768, 0, 768, 400);
        } 
    }
2

2 Answers

3
votes

I think you should change the contentOffset of the scrollview when rotation is taking place. You should have a way to know which page is currently displayed before rotation (maybe put this information in a variable). Then in your didRotate.. method set the contentOffset of the scrollview after resizing it, like this:

CGFloat offset = self.scrollView.frame.size.width * currentPageIndex;
[self.scrollView setContentOffset: offset];
1
votes

As an alternative to laying out your subviews in your view controller, have you considered subclassing your UIScrollView and overriding it's layoutSubviews method? You might also consider defining your dimensions as percentages rather than fixed points - because the point values will shift according to rotation and presence of other UI elements such as navigation and toolbars. You may run into trouble as you're manually resizing UI elements in your rotation method, at the same time that the UI is going to be attempting to automatically resize elements according to your resizing masks. Just my thought...