2012-01-27 31 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

相關問題