2012-01-27 8 views
1

彼女の料理本の例Ch07-11で、Erica Sadunが(viewDidLayoutSubviewsでviewDidAppearを呼び出す)理由を理解するのが難しいです。おそらく、2つのメソッドは代わりに別のメソッドを呼び出す必要がありますか?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

私はこれが悪いコードを分解していると思います。彼女は初期レイアウトを行うためにviewDidAppearにUIViewControllersシステムコールを使用しています。そして、ビューがサブビューのレイアウトを終えたときに同じメソッドを直接遅延して再利用しています。私は、viewDidAppearは 'adjustView'やviewDidLayoutSubviewsのようなメソッドを呼び出すべきであるという前提で正しいと思います。 – RLB

+1

私はviewDidLayoutSubviewsにすべてのレイアウトを入れることができ、viewDidAppear、viewWillAppear、またはdidRotateFromInterfaceOrientationにする必要はないことを発見しました。 –

答えて

3

これはちょうど私に間違っています。これらのメソッドが呼び出されたときにUIKitを処理させます。 viewDidLayoutSubviews内のレイアウトsrollViewがscrollViewが少し吃音を取得する必要がありますスクロール、あなたはup.ThenをsettedいたscrollViewが変わりますので

 
- (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

は、代わりにこれを行います。

+0

これは質問に対する答えを提供しません。十分な[評判](http://stackoverflow.com/help/whats-reputation)があれば、どの投稿でも[コメント](http://stackoverflow.com/help/privileges/comment)できるようになります。また、これを確認してください(代わりに何ができますか?)(https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reationation-to-comment-what-c​​an-i-do-instead )。 – thewaywewere

関連する問題