Praveen Sharma
Praveen Sharma

Reputation: 181

UIScrollView Subview Container Incorrectly Changing Frames

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:

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));
        }
    }

Upvotes: 3

Views: 2535

Answers (2)

Baljeet Singh
Baljeet Singh

Reputation: 453

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

Upvotes: 3

matt
matt

Reputation: 535121

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.

Upvotes: 3

Related Questions