3

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));
        }
    }
Praveen Sharma
  • 181
  • 2
  • 12

2 Answers2

3

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Well the origin of this problem lies in a complex framework I built which can support building applications dynamically. The 71 VCs in question are theoretical container VCs which can load and unload their views when necessary. They can be any structure. Everything is generated in code - no NIBs but I will double check the generic auto generated nib now. I will also try translatesAutoResizingMask now! Thanks. – Praveen Sharma Dec 24 '12 at 21:13
  • 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. – Praveen Sharma Dec 24 '12 at 21:19
  • i have same issue , i tried 'translatesAutoresizingMaskIntoConstraints' but not working ,Please help – Sanoj Kashyap Jul 27 '13 at 19:49
3

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

Baljeet Singh
  • 453
  • 5
  • 15