3

UIViewビューアピアランスチェーン中のフレームや、ストーリーボードと従来のxibのフレームに関連する違いについて、いつ安全に質問できるかわかりません。

Xcode4.5.1で2つの異なる非常に単純な単一UIViewControllerプロジェクトを作成しました。最初はxibを使用した標準のシングルビューテンプレートを使用し、2番目はストーリーボードを使用しました。のソースコードViewController.m–両方のプロジェクトで同一–を以下に示します。IBでは、をドラッグしUIScrollViewて、コントローラーのコンセントに適切に配線しました。

ソースでわかるように、ビューの外観チェーンのさまざまなポイントでスクロールビューのフレームをログアウトしています。の(0,0)であるため、なぜそれらが異なるのか、および/またはUIScrollViewフレームを安全にクエリできるのはいつかわかりませんviewDidLoad

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"viewDidLoad scrollView.frame = (%3.0f,%3.0f)",self.scrollView.frame.size.width,self.scrollView.frame.size.height);
}
- (void)viewWillLayoutSubviews {
    NSLog(@"viewWillLayoutSubviews scrollView.frame = (%3.0f,%3.0f)",self.scrollView.frame.size.width,self.scrollView.frame.size.height);    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear scrollView.frame = (%3.0f,%3.0f)",self.scrollView.frame.size.width,self.scrollView.frame.size.height);

}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"viewDidAppear scrollView.frame = (%3.0f,%3.0f)",self.scrollView.frame.size.width,self.scrollView.frame.size.height);
}
@end

xibでの結果

2012-10-23 11:49:48.316 ScrollTest[83262:c07] viewDidLoad scrollView.frame = (320,548)
2012-10-23 11:49:48.318 ScrollTest[83262:c07] viewWillAppear scrollView.frame = (320,548)
2012-10-23 11:49:48.321 ScrollTest[83262:c07] viewWillLayoutSubviews scrollView.frame = (320,548)
2012-10-23 11:49:48.328 ScrollTest[83262:c07] viewDidAppear scrollView.frame = (320,460)

ストーリーボードでの結果

2012-10-23 11:49:58.762 ScrollTestStoryboard[83308:c07] viewDidLoad scrollView.frame = (  0,  0)
2012-10-23 11:49:58.763 ScrollTestStoryboard[83308:c07] viewWillAppear scrollView.frame = (  0,  0)
2012-10-23 11:49:58.765 ScrollTestStoryboard[83308:c07] viewWillLayoutSubviews scrollView.frame = (  0,  0)
2012-10-23 11:49:58.772 ScrollTestStoryboard[83308:c07] viewDidAppear scrollView.frame = (320,460)
4

1 に答える 1

4

答えはviewDidLayoutSubviewsです。20分ほど前に見たかったのに。以下は、更新されたソースコードとログ出力です。

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"viewDidLoad scrollView.frame = (%3.0f,%3.0f)",self.scrollView.frame.size.width,self.scrollView.frame.size.height);
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear scrollView.frame = (%3.0f,%3.0f)",self.scrollView.frame.size.width,self.scrollView.frame.size.height);

}
- (void)viewWillLayoutSubviews {
    NSLog(@"viewWillLayoutSubviews scrollView.frame = (%3.0f,%3.0f)",self.scrollView.frame.size.width,self.scrollView.frame.size.height);
}
- (void)viewDidLayoutSubviews {
    NSLog(@"viewDidLayoutSubviews scrollView.frame = (%3.0f,%3.0f)",self.scrollView.frame.size.width,self.scrollView.frame.size.height);
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"viewDidAppear scrollView.frame = (%3.0f,%3.0f)",self.scrollView.frame.size.width,self.scrollView.frame.size.height);
}
@end

ストーリーボード出力

2012-10-23 12:12:35.597 ScrollTestStoryboard[87593:c07] viewDidLoad scrollView.frame = (  0,  0)
2012-10-23 12:12:35.599 ScrollTestStoryboard[87593:c07] viewWillAppear scrollView.frame = (  0,  0)
2012-10-23 12:12:35.601 ScrollTestStoryboard[87593:c07] viewWillLayoutSubviews scrollView.frame = (  0,  0)
2012-10-23 12:12:35.602 ScrollTestStoryboard[87593:c07] viewDidLayoutSubviews scrollView.frame = (320,460)
2012-10-23 12:12:35.609 ScrollTestStoryboard[87593:c07] viewDidAppear scrollView.frame = (320,460)
于 2012-10-23T16:20:31.623 に答える