views:

39

answers:

2

I have a scrollview that automatically generates a series of subviews containing imageviews. The concept is that it's pretty much a custom PDF style reader, where each page is loaded in to an imageview as an image. There are three "layers" - the outer UIScrollView, the automatically generated subViews and the imageview inside the subview.

My dilemma is that as these subviews are generated they don't really have a hard reference, so in my didRotateFromInterfaceOrientation there's no way to say resize THIS particular view.

I have created a function called setUpView which initialises all the views and images as so:

- (void)setUpView {
    //INITIALISE PAGING SCROLL VIEW
    CGRect pagingScrollViewFrame = [[UIScreen mainScreen] bounds];
    pagingScrollViewFrame.origin.x -= 10;
    pagingScrollViewFrame.size.width += 20;
    pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];

    //CONFIGURE PAGING SCROLL VIEW  
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor blackColor];
    pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width*7, pagingScrollViewFrame.size.height);

    //ACTIVATE PAGING SCROLL VIEW
    self.view = pagingScrollView;

    //ADD PAGES TO SCROLL VIEW
    for (int i = 0; i < 7; i++){
        ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
        [self configurePage:page forIndex:i];
        [pagingScrollView addSubview:page];
    }
}

In my loadView method I just call this function.

Is there any way to detect the current orientation of the device so that may be fed into the above function somehow? At the moment, on rotate, the outerview rotates fine, but the inner sub-views and the imageviews do not.

+1  A: 

You can get device orientation by looking at:

[UIDevice currentDevice].orientation

or you could use self.interfaceOrientation for viewController.

Cheers, Krzysztof Zabłocki

Krzysztof Zabłocki
so I could have an if statement that says something like if(self.interfaceOrientation = landscapeleft){ //changedimensions };
Daniel Hanly
yep, you could use layoutSubviews function to update your subviews according to current orientation ( it will get called when frame changes [ when you rotate frame usually changes :) ]
Krzysztof Zabłocki
+1  A: 

Hello Daniel,

You can make the objects without reference observer for UIDeviceOrientationDidChangeNotification notification, which will be posted on every orientation change.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

Then, in your orientationChanged: method you can check current orientation and layout your views accordingly.

However, you need to tell the device to generate such notifications by calling

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Be sure to remove the objects that are observing the orientation change on dealloc!

Hope this was helpful, Pawel

Pawel