我正在创建类似于 SplitFrameViewController 的东西,但有足够的差异,我正在制作自定义实现。当我试图将“主”视图的大小设置为可用屏幕的 25% 并将“详细信息”视图设置为剩余的 75% 时,我的问题就出现了。
不管我怎么尝试,尺寸都不正确。MasterView 太宽了,Detail 重叠了一点。我想我可能在我的 nib 文件中做错了什么,但我不太确定。在 iPad 模拟器上,主视图的尺寸约为 35%,细节视图的尺寸约为 80%。
另外,如果有人对我的项目结构有任何批评/意见/建议,请随时发表评论,因为这是我的第一次尝试。
应用委托
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.containerViewController = [[ContainerViewController alloc] init];
self.window.rootViewController = self.containerViewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
容器视图控制器
我检查了 中生成的矩形的宽度viewWillAppear
,它们是正确的。
- (id)init
{
self = [super init];
if (self) {
self.masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
[self addChildViewController:self.masterViewController];
[self addChildViewController:self.detailViewController];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.masterViewController.view];
[self.masterViewController didMoveToParentViewController:self];
[self.view addSubview:self.detailViewController.view];
[self.detailViewController didMoveToParentViewController:self];
}
- (void)viewDidUnload
{
self.masterViewController = nil;
self.detailViewController = nil;
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGRect containerBounds = self.view.bounds;
CGRect masterBounds, detailBounds;
CGRectDivide(containerBounds, &masterBounds, &detailBounds, CGRectGetWidth(containerBounds) * 0.25, CGRectMinXEdge);
self.masterViewController.view.frame = masterBounds;
self.detailViewController.view.frame = detailBounds;
}
编辑
我在末尾添加了以下几行viewWillAppear
并得到了注释结果。
NSLog(@"container bounds: %@", NSStringFromCGRect(containerBounds));
NSLog(@"master bounds: %@", NSStringFromCGRect(masterBounds));
NSLog(@"detail bounds: %@", NSStringFromCGRect(detailBounds));
// container bounds: {{0, 0}, {768, 1004}}
// master bounds: {{0, 0}, {192, 1004}}
// detail bounds: {{0, 0}, {576, 1004}}