2012-01-27 30 views
1

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

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

任何想法,为什么?

+3

我想这只是不好的代码因子。她正在使用UIViewControllers系统调用viewDidAppear来执行初始布局,然后当视图完成布局子视图时,直接懒懒地重新使用相同的方法。我认为你的假设是正确的,viewDidAppear应该调用像'adjustView'这样的方法,viewDidLayoutSubviews也应该这样做。 – RLB 2012-01-28 17:09:08

+1

我发现我可以将所有布局的东西放在viewDidLayoutSubviews中,并且它不需要处于viewDidAppear,viewWillAppear或甚至didRotateFromInterfaceOrientation中。 – 2013-09-30 14:55:05

回答

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

因为viewDidLayoutSubviews布局srollView将改变滚动视图,你已经设置好的up.Then,滚动滚动视图应该得到小口吃。

+0

这不提供问题的答案。一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你将可以在任何帖子上[评论](http://stackoverflow.com/help/privileges/comment)。另外检查这[我可以做什么,而不是](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an-i-do-instead )。 – thewaywewere 2017-05-31 03:54:58

相关问题