Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
I have a scrollview in UIViewController and it is attached with contstraints with 0 constant to all 4 edges. I am doing this in viewDidLoad
Code:
if (self.allSizeModel != nil) {
        [self setChildViewControllers];
}

and here is the method
Code:
- (void)setChildViewControllers {
  
    topVC = [[MySizesTopShoeVC alloc]initWithNibName:mad:"MySizesTopShoeVC" bundle:nil];
    topVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-55);
    topVC.delegate = self;
    [topVC.dataArray addObjectsFromArray:self.allSizeModel.Top];
    [self addChildViewController:topVC];
    [self.containerScrollView addSubview:topVC.view];
    [topVC didMoveToParentViewController:self];
  
    bottomVC = [[MySizesBottomVC alloc]initWithNibName:mad:"MySizesBottomVC" bundle:nil];
    bottomVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-55);
    bottomVC.delegate = self;
    [bottomVC.dataArray addObjectsFromArray:self.allSizeModel.Bottom];
    [self addChildViewController:bottomVC];
    [self.containerScrollView addSubview:bottomVC.view];
    [bottomVC didMoveToParentViewController:self];
  
    shoeVC = [[MySizesShoeVC alloc]initWithNibName:mad:"MySizesShoeVC" bundle:nil];
    shoeVC.view.frame = CGRectMake(2*SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-55);
    shoeVC.delegate = self;
    [shoeVC.dataArray addObjectsFromArray:self.allSizeModel.Shoe];
    [self addChildViewController:shoeVC];
    [self.containerScrollView addSubview:shoeVC.view];
    [shoeVC didMoveToParentViewController:self];
  
    brandVC = [[MySizesBrandVC alloc]initWithNibName:mad:"MySizesBrandVC" bundle:nil];
    brandVC.view.frame = CGRectMake(3*SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-55);
    brandVC.delegate = self;
    [self addChildViewController:brandVC];
    [self.containerScrollView addSubview:brandVC.view];
    [brandVC didMoveToParentViewController:self];
    brandVC.isFromProfile = true;
}

but all view controllers that are added as child have wrong frames. I tried to call that method in viewDidLayoutSubviews then the scroll view didnt work properly.
How can I make the frames are calculated correctly?
 
Move it to viewDidAppear.

I found often when adding subviews the parent view has to actually be on screen for the correct frame and bounds to be determined.
 
  • Like
Reactions: ConvertedToMac
You should use auto layout to position the child view controllers' views. When I've done this I usually have placeholder UIViews in the view controller's view that are set up with the appropriate constraints in the storyboard. Then when adding the child view controllers I add simple constraints that just pin the child views to their placeholder views' four sides.

As mentioned above the frames of the various views change from loadView, viewDidLoad, viewWillAppear, viewDidAppear and after that as well if the device rotates. If you set up the correct constraints the views should still be in their correct positions.
 
  • Like
Reactions: ConvertedToMac
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.