gpt4 book ai didi

cocoa-touch - Storyboard 和 xib 在 View 外观过程中的不一致

转载 作者:行者123 更新时间:2023-12-01 12:50:13 24 4
gpt4 key购买 nike

我不明白什么时候可以安全地询问 View 外观链中 UIView 的框架和/或 Storyboard 与传统 xib 的相关差异。

我在 Xcode 4.5.1 中创建了两个不同的、非常简单的单个 UIViewController 项目;在第一个中,我使用了带有 xib 的标准单 View 模板,在第二个中,我使用了 Storyboard。 ViewController.m 的源代码(两个项目相同)如下所示。在 IB 中,我拖入了一个 UIScrollView 并将其适本地连接到我的 Controller socket 。

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

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)

Storyboard的结果

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)

最佳答案

答案是 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

Storyboard输出

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)

关于cocoa-touch - Storyboard 和 xib 在 View 外观过程中的不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13034737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com