Question

I'm having a problem with ChildViewController views having their frames automatically changed to their parents' frames. I've distilled it to an example project with a single class to illustrate whats happening.

In a nutshell, I:

  • have a ScrollViewController
  • add ScrollViewController as the RootViewController and add to Window
  • In the ScrollViewController - at ViewDidLoad I create a Container UIView with a frame of {{0, 0}, {72704, 768}}
  • Create a UIScrollView with a frame of {{0, 0}, {1024, 768}}
  • Add the ScrollView to the ScrollViewController View
  • Add the Container to the ScrollView
  • Create 71 UIViewControllers with a frame of {{0, 0}, {1024, 768}}
  • Add them as ChildViewControllers of the ScrollViewController
  • Add their subviews to the Container
  • Log all 71 frames on ViewDidAppear
  • Each frame is {{0, 0}, {72704, 768}} - NOT 1024 x 768 which I set.

All autoresizing mask / autoresizes subviews set to NO.

This is running on iOS 6 but also occurs on iOS 5.1!

UPDATE! It seems like addChildViewController is the culprit somehow. Not sure why - is this expected behavior? I need to be able to add these childViewControllers to the ScrollViewController without their views frames being automatically scaled to their parents'. Isn't this theoretically the way to do it?

Also - no NIBs anywhere. Set "translatesAutoresizingMaskIntoConstraints" to NO on the Scrollview / ContainerView and each of the 71 VC Views. Same thing, unfortunately. This seems like very strange behavior...

Link to project - https://dl.dropbox.com/u/637000/TestingBug.zip

CODE FROM ScrollViewController:

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        [self initScrollView];
        [self createTestViews];
    }

    - (void)initScrollView {
        if(scrollView == nil) {
            containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 72704, 768)];
            containerView.autoresizingMask = UIViewAutoresizingNone;
            containerView.autoresizesSubviews = NO;

            scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
            scrollView.autoresizingMask = UIViewAutoresizingNone;
            scrollView.showsHorizontalScrollIndicator = NO;
            scrollView.showsVerticalScrollIndicator = NO;
            scrollView.autoresizesSubviews = NO;
            scrollView.delegate = self;

            [self.view addSubview:scrollView];
            [scrollView addSubview:containerView];
        }
    }

    - (void)createTestViews {
        for(int count = 0; count < 71; count++) {
            [self createTestView];
        }
    }

    - (void)createTestView {
        UIViewController *testVC = [[UIViewController alloc] init];
        testVC.view.frame = CGRectMake(0, 0, 1024, 768);

        [self addChildViewController:testVC];
        [containerView addSubview:testVC.view];
        [testVC didMoveToParentViewController:self];
    }

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];

        for(UIView *subview in containerView.subviews) {
            // THIS IS {{0, 0}, {72704, 768}} NOT 1024 x 768
            NSLog(@"BROKEN FRAME = %@", NSStringFromCGRect(subview.frame));
        }
    }
Was it helpful?

Solution

In IOS 7.0 Use self.automaticallyAdjustsScrollViewInsets=NO; in ViewDidLoad

OTHER TIPS

Create 71 UIViewControllers with a frame of {{0, 0}, {1024, 768}}

Whoa! This sound like a totally unnecessary use of UIViewController. Can you manage without it? The usual thing is to add views directly to a scroll view, without any intervening UIViewController containment foo.

To grapple more directly with your question: might autolayout be involved? (The answer is yes by default for any new nib created in Xcode 4.5.) I don't see you setting translatesAutoresizingMaskIntoConstraints to NO, so if autolayout is playing a part perhaps it's resizing the views. Of course I also don't see you setting any autoresizingMask to start with, so you're getting a default value that you aren't taking charge of.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top