1

I am having difficulty figuring out why Erica Sadun does the following (calling viewDidAppear in viewDidLayoutSubviews) in her cookbook example Ch07-11. Perhaps the two methods should be calling another method instead?

See: https://github.com/erica/iOS-5-Cookbook/tree/master/C07

- (void) viewDidAppear:(BOOL)animated
{
    scrollView.frame = self.view.bounds;
    scrollView.center = CGRectGetCenter(self.view.bounds);

    if (imageView.image)
    {
        float scalex = scrollView.frame.size.width / imageView.image.size.width;
        float scaley = scrollView.frame.size.height / imageView.image.size.height;
        scrollView.zoomScale = MIN(scalex, scaley);
        scrollView.minimumZoomScale = MIN(scalex, scaley);
    }
}

- (void) viewDidLayoutSubviews
{
    [self viewDidAppear:NO];
}

Any idea why?

RK-
  • 12,099
  • 23
  • 89
  • 155
Boon
  • 40,656
  • 60
  • 209
  • 315
  • 3
    I would guess this is just bad code factoring. She is using the UIViewControllers system call to viewDidAppear to do initial layout, and then lazily re-using the same method directly when the view is finished laying out subviews. I think you are correct in your assumption that viewDidAppear should be calling a method like 'adjustView' and viewDidLayoutSubviews should be doing the same. – RLB Jan 28 '12 at 17:09
  • 1
    I have found that I can put all the layout stuff in viewDidLayoutSubviews and it doesn't need to be in viewDidAppear, viewWillAppear, or even didRotateFromInterfaceOrientation. – Bill Cheswick Sep 30 '13 at 14:55

2 Answers2

3

This just seems flat out wrong to me. Let UIKit handle when these methods get called.

Do this instead:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated]; // Always call super with this!!!

    [self doSomeCustomLayoutStuff]; // I don't actually think this is necessary. viewWillLayoutSubviews is meant for laying out subviews, and gets called automatically in iOS 5 and beyond.

}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    [self doSomeCustomLayoutStuff];
}

- (void)doSomeCustomLayoutStuff {
    scrollView.frame = self.view.bounds;
    scrollView.center = CGRectGetCenter(self.view.bounds);

    if (imageView.image)
    {
        float scalex = scrollView.frame.size.width / imageView.image.size.width;
        float scaley = scrollView.frame.size.height / imageView.image.size.height;
        scrollView.zoomScale = MIN(scalex, scaley);
        scrollView.minimumZoomScale = MIN(scalex, scaley);
    }
}
Jared Egan
  • 1,268
  • 1
  • 12
  • 19
0

because layout srollView in viewDidLayoutSubviews will change scrollView which you had setted up.Then, scrolling scrollView should get little stutters.

wakakaJP
  • 11
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment](http://stackoverflow.com/help/privileges/comment) on any post. Also check this [what can I do instead](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – thewaywewere May 31 '17 at 03:54