我不明白什么时候在视图外观链中询问 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)