UIViewController viewDidLayoutSubviews

时间:2012-01-27 23:22:10

标签: ios viewdidlayoutsubviews

我很难弄清楚为什么Erica Sadun在她的食谱示例Ch07-11中执行以下操作(在viewDidLayoutSubviews中调用viewDidAppear)。也许这两种方法应该调用另一种方法呢?

请参阅: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];
}

知道为什么吗?

2 个答案:

答案 0 :(得分:3)

这对我来说似乎很不对劲。让UIKit在调用这些方法时处理。

请改为:

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

答案 1 :(得分:0)

因为viewDidLayoutSubviews中的布局srollView将更改您已设置的scrollView。然后,滚动scrollView应该得到一点口吃。