3

我不明白什么时候在视图外观链中询问 a 的框架UIView和/或与 Storyboard 与传统 xib 相关的差异是安全的。

UIViewController我在 Xcode 4.5.1 中创建了两个不同的、非常简单的单个项目;在第一个中,我使用带有 xib 的标准单视图模板,在第二个中,我使用了 Storyboard。ViewController.m下面给出了两个项目相同的源代码。在 IB 中,我拖入 aUIScrollView并将其适当地连接到我的控制器插座。

正如您将在源代码中注意到的那样,我正在视图外观链中的不同点注销滚动视图的框架。我不明白为什么它们不同和/或什么时候我可以安全地查询 UIScrollView 框架,因为它在viewDidLoad.

视图控制器.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 分钟前看到。下面是更新的源代码和日志输出。

视图控制器.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 回答